C++ 将指针传递给"multiple"类的矢量
c++ pass pointer to vector for "multiple" class
嘿伙计们,我需要将一个向量点传递给一个类的构造函数,并在构造器中将这一点传递给其他构造函数,
/*======================== 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。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 使用指向成员的指针将成员函数作为参数传递
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- C++ 将指针传递给"multiple"类的矢量