我有一个非常小的选项处理功能,我需要经常输入这个功能。所以我不能分配矢量。我需要能够向向量添加一个元素并保存它,所以当我回到这个函数时它仍然是它们。
vector<sStruct> * loadFile(char *myTextFile)
{
myStruct
sStruct;
vector<myStruct>
vectorAddress,
vectorData;
vectorData = &vectorAddress;
string
feild1, feild2, feild3, feild4;
ifstream
*inFile = new ifstream;
inFile->open( myTextFile, ios::in );
if ( !inFile->good() )
{
cout << "? File Doesnt Exist! " << endl;
}
while ( !inFile->eof() )
{
// reading a file deliimted by commas hi,hello,hey,1234
//...
getline( *inFile, feild1, ',' );
sStruct.m_1 = field1;
getline( *inFile, feild2, ',' );
sStruct.m_2 = field2;
getline( *inFile, field3, ',' );
sStruct.m_3; = feild3
getline( *inFile, feild4 );
sStruct.m_4 = feield4;
// saving each member of the struct in the vector
vectorAddress.push_back( sStruct );
}
inFile->clear();
inFile->close();
cout << vectorAddress.size() << endl;
delete inFile;
//
(*vectorData) = vectorAddress;
return vectorData;
}
// This function tries despretly to add another element saved in struct member varaible
// to the end of the vector. I need the information from the first function to be here. What i think im trying to do is refer to the same address in memory.
vector<sStruct> * addElement(vector<sStruct> *vAddElement)
{
myStruct sAddElement; // referring to the same struct.
vector<sStruct> vectorAddress;
vAddElement = &vectorAddress;
cout << "Enter a String: ";
cin >> sAddElement.feild1 // save user spec in struct member
vectorAddress.push_back( sAddElement );
cout << vectorAddress.size() << endl;
(*vAddElement) = vectorAddress;
return vAddElement;
}
我试图在不改变功能签名的情况下这样做。
答案 0 :(得分:3)
首先,对这类事物使用引用,它减少了不必要的代码,不能为NULL。
在问题上,要添加到矢量,使用push_back或其他一些添加到矢量的方法,你所做的就是尝试分配给矢量:
vector<sStruct> *addElement(vector<sStruct> &vAddElement) {
myStruct sAddElement; // referring to the same struct.
cout << "Enter a String: ";
cin >> sAddElement.feild1 // save user spec in struct member
vAddElement.push_back(sAddElement);
cout << vectorAddress.size() << endl;
return &vAddElement; // since you said you must return a pointer (which is silly)
// we'll return the address of the object passed in.
}
编辑:为什么你这么多指针(根本没有)你的绝对没有必要 用于任何动态分配或获取成员地址的程序。通常的堆栈分配和通过引用传递将允许您使用一半代码(并且正确)执行相同的工作。
编辑:此外,您的循环已损坏,您无法正确测试EOF,直到您尝试读取后 。这样做更容易:
while(getline(file, line)) { /* process line */ }
修改强>
你的这段代码也与你的想法差不多, 我会为你推荐,所以你知道:
vector<sStruct> *addElement(vector<sStruct> *vAddElement) {
myStruct sAddElement;
// ...
vector<sStruct> vectorAddress; // creates a new *vector* on the stack
vAddElement = &vectorAddress; // makes vAddElement point to the new vector
// but does *not* effect the vector whose
// address you passed
// ...
vectorAddress.push_back( sAddElement ); // adds an element to the new vector
cout << vectorAddress.size() << endl; // it's size will always be 1,
// since you just added the first element
(*vAddElement) = vectorAddress; // does absolutely nothing, you are assigning
// the new vector to itself
return vAddElement; // erroneously returns a pointer to the new vector
// it was allocated on the stack and no longer exists
// after the return, **never do that **
}