【问题标题】:How to build a constructor for a struct within a class如何为类中的结构构建构造函数
【发布时间】: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&lt;Tri_for_one_lan&gt;吗?
  • //There are 19683 possible trigrams 后跟capacity = 1000; 的目的是什么?
  • 你的构造函数没问题(虽然没有必要)。问题出在代码的其他地方。您需要发布一个完整的程序,它显示问题。没有人知道你的程序在说什么。 Please read this page

标签: c++ class struct constructor initialization


【解决方案1】:

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

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

【讨论】:

  • 是的,在我按照你说的启动它之后。我遇到了段错误
  • @NikkyXiong 一开始我没有意识到它是一个动态数组——我的错。考虑我的编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 2013-01-16
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 2010-11-10
相关资源
最近更新 更多