當前正在將我的程序中的一個從Matlab
遷移到C++
,因此我在閱讀file.csv
時遇到困難,並且尋求幫助以幫助理解。C++ LNK2001:Struct數組無法解析的外部符號
struct nav {
std::string title;
... // I have 17 members but for simplicity purposes I am only disclosing
// two of them
float quant;
};
nav port[];
std::string filedir = "C:\\local\\";
std::string fdbdir = filedir + "Factor\\";
std::string extension1 = "fdb.csv";
std::string extension2 = "nav.csv";
std::string factorpath = fdbdir + extension1;
std::string factorpath2 = filedir + extension2;
std::ifstream fdbdata(factorpath);
std::ifstream navdata(factorpath2);
int main() {
// 2nd data file involving data of different types
{
navdata.open(factorpath2);
if (navdata.fail()) {
std::cout << "Error:: nav data not found." << std::endl;
exit(-1);
}
for (int index = 0; index < 5; index++)
{
std::getline(navdata, port[index].title, ',');
std::getline(navdata, port[index].quant, ',');
}
for (int index = 0; index < 4; index++)
{
std::cout << port[index].title << " " << port[index].quant <<
std::endl;
}
}
}
錯誤:LNK2001: unresolved external symbol "struct nav * port" ([email protected]@[email protected]@A)
從Error list
,肯定是有什麼問題,我想知道的struct type
port
聲明。
最重要的是:有沒有一種方法不硬編碼index
作爲數據的維度不固定。我用for (int index = 0; index < 4; index++)
用於測試目的,但index
可以是任何整數爲50200,等
編輯:
按照要求,請找小例子,下面:
struct Identity {
int ID;
std::string name;
std::string surname;
float grade;
};
std::string filedir = "C:\\local\\";
std::string extension = "sample.csv";
std::string samplepath = filedir + extension;
int main() {
std::ifstream test(samplepath);
std::vector<Identity> iden;
Identity i;
while (test >> i.ID >> i.name >> i.surname >> i.grade)
{
iden.push_back(i);
}
std::cout << iden[1].name;
system("pause");
}
導致vector subscript out of range
。有什麼想法在這裏看起來不對?
另外,下面的樣本數據的要求: PS:所述點頭應該被解讀級一致性的目的。
最佳,
'nav port [];'< - 這是什麼? – Drop
可能幫助:[The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Drop
你不能定義一個「空」陣列。如果你想添加運行時然後使用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)。 –