2015-12-04 3 views
-1

Я попытался построить конструктор для структуры 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; 
} 
+2

Формат вашего кода. Это беспорядок. –

+0

Где вы инициализируете свою структуру? Можете ли вы показать пример того, как вы это делаете? (откуда появляются неожиданные результаты) –

+0

'trigram' - это указатель. Вы должны выделить память для него в конструкторе TrigramVector. –

ответ

0

Рассматривали ли вы хранить trigram в vector? Вектор представляет собой динамический массив из стандартной библиотеки шаблонов.

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

Да, после того, как я инициирую это, как вы сказали. У меня возникла ошибка seg –

+0

@NikkyXiong Я не понимал, что это был динамический массив сначала - моя ошибка. Рассмотрим мое редактирование. –

Смежные вопросы