【发布时间】:2016-03-08 21:13:58
【问题描述】:
我尝试在名为TrigramVector 的类中为结构Tri_for_one_lan 构建构造函数。但是,它不起作用,因为我得到的所有cout 都是一堆内存地址。在我构建构造函数来初始化结构之前,一切都很好。
这是我的 .h 文件:
#include <iostream>
#include <fstream>
using namespace std;
struct Tri_for_one_lan{
string name;
int freq;
int total;
//a constructor to initialize values of the dynamic array
Tri_for_one_lan();
};
class TrigramVector
{
public:
//a constructor to initialize the values of the dynamic array
TrigramVector();
//to make modifications to the chracters in the files
TrigramVector process_files(string filename);
//store three characters of the string into the array
void store_trigrams(int sLength, string compress_spaces);
void expand(); //double the capacity if it runs out of spaces
//print out the filename and trigrams in the file
void report();
//print out the frquency of the trigrams for each language and
//the total number of the trigrams
void print_freq(string language);
private:
//a dynamic array to store all the trigram
Tri_for_one_lan *trigram;
int used;
int capacity;
//to make all characters to lowercases
string get_to_lower(string filename, string original);
//to compress multiple spaces into one space
string get_one_space(int sLength, string original,
string compare_spaces);
//check if this trigram has already existed
bool is_appeared(string temp);
};
这是我的 .cpp 文件:
#include <iostream>
#include <fstream>
#include "TrigramVector.h"
using namespace std;
//
// TrigramVector--a constructor that set initial default value
// for the TrigramVector
// args: none
// rets: nothing
// does: initial the values of used, capacity, and the array
//
TrigramVector::TrigramVector(){
used = 0;
//There are 19683 possible trigrams
capacity = 1000;
}
//
// Tri_for_one_lan--a constructor that set initial default value
// for the TrigramVector
// args: none
// rets: nothing
// does: initial the values of name, frequency, and the total trigrams
//
Tri_for_one_lan::Tri_for_one_lan(){
name = "";
freq = 1;
total = 0;
}
【问题讨论】:
-
格式化您的代码。这是一团糟。
-
你在哪里初始化你的结构?你能举一个你这样做的例子吗? (意想不到的结果来自哪里)
-
trigram是指只保存一个Tri_for_one_lan还是一组它们?你考虑过std::vector<Tri_for_one_lan>吗? -
//There are 19683 possible trigrams后跟capacity = 1000;的目的是什么? -
你的构造函数没问题(虽然没有必要)。问题出在代码的其他地方。您需要发布一个完整的程序,它显示问题。没有人知道你的程序在说什么。 Please read this page
标签: c++ class struct constructor initialization