C++ 将指针传递给"multiple"类的矢量

c++ pass pointer to vector for "multiple" class

本文关键字:multiple 指针 C++      更新时间:2023-10-16

嘿伙计们,我需要将一个向量点传递给一个类的构造函数,并在构造器中将这一点传递给其他构造函数,

/*======================== Class Dados/Data ========================*/
class Dados
{
    string nome;
    int valor;
public:
    Dados(string n, int v) : nome(n), valor(v){};
    //~dados();
    string GetNome()const{return nome;}
    int GetValor()const{return valor;}
    void SetValor(int x){valor = x;}

    string GetAsString() const {
        ostringstream oss;
        oss << nome <<": "<< valor;
        return oss.str();
    }
};

/*======================== Class FileReader ========================*/
class FileReader{
    vector<Dados> dados;
public:
    bool ReadFile(string file) {
        dados.empty();
        string fnome, ftemp;
        int fvalor;
        ifstream fich(file);
        string linha;

        if (fich.is_open())                 
        {
            while (fich.peek() != EOF){
                getline(fich, linha);
                istringstream iss(linha);
                //cout << ".";
                iss >> fnome;               
                iss >> ftemp;
                iss >> fvalor;
                dados.push_back(Dados(fnome,fvalor));
            }

            fich.close();
            return 0;
        }
        else{ 
            cout << "Ficheiro ""<< file <<"" nao encontrado!";
            return 1;
        }
    }

    string GetAsString() 
    {
        ostringstream oss;
        vector<Dados>::const_iterator it;
        it = dados.begin();
        while (it != dados.end()){
            oss << it->GetAsString() << endl;
            it++;
        }
        return oss.str();
    }

    int FindOnVector(string const fi)
    {
        int val = -1; //-1 se for erro
        vector<Dados>::const_iterator it;
        it = dados.begin();
        while (it != dados.end()){
            if(fi == it->GetNome()){
                val = it->GetValor();
            }
            it++;
        }
        return val;
    }

    void Save(){
        vector<Dados>::const_iterator it;
        it = dados.begin();
        while (it != dados.end()){
        cout << it->GetAsString() << endl;
        it++;
        }
    }
};

在主要我有,

void main(int argc,char** argv){
    FileReader fr;

    if( fr.ReadFile(argv[1])==0){
        cout <<endl<< fr.GetAsString();
    }else {
        cout << "[ ERRO ]" << endl;
    }
    jogo g(&fr);
    cout << "nn-------------Test2--------------------" << endl;
    fr.Save();
}

另外两个类:

class jogo{

public:
    jogo(FileReader *A){
        teste t(&A);

    };

};
class teste{
public:
    teste(FileReader *C){
        vector<Dados>::const_iterator it;
        it = C.begin();
        while (it != C.end()){
            cout << it->GetAsString() << endl;
            it++;
        }
    };

};

在 Jogo 类中,我尝试将 FileReader 的点传递给测试类,但是不可能的。我做错了什么?

抱歉,这不是必要的具体内容。

错误:在课堂测试中当我尝试使用C的指针时,我会出现此错误:

1   IntelliSense: expression must have class type   e:isDocumentsConsoleApplication1ConsoleApplication1Source.cpp  141

最好。

哦,我明白了:您很可能会在调用teste构造函数时出错,因为您缺少对该类的 Forward 引用。 对两个类使用 .h 文件或在 jogo 之前声明 teste。