如何为类中的结构构建构造函数

时间:2015-12-04 02:19:01

标签: c++ class struct constructor initialization

我尝试在名为Tri_for_one_lan的类中为结构TrigramVector构建构造函数。但是,它不起作用,因为我得到的所有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;
}

1 个答案:

答案 0 :(得分:0)

您是否考虑将trigram存储在vector中?矢量是标准模板库中的动态数组。

//a dynamic vector to store all the trigram
std::vector<Tri_for_one_lan> trigram;