[英]C++ LNK2001: unresolved external symbol with Struct Array
目前正在將我的一個程序從Matlab
遷移到C++
,我在閱讀file.csv
時遇到了困難,並尋求幫助以幫助我理解。
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" (?port@@3PAUnav@@A)
從Error list
,我想知道的struct type
port
的聲明肯定有問題。
最重要的是:由於數據的維度不固定,是否有一種不硬編碼index
方法。 我已經將for (int index = 0; index < 4; index++)
用於測試目的,但index
可以是任何整數,如 50,200 等。
編輯:
根據要求,請在下面找到最小示例:
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:為了一致性目的,點標題應為讀取等級。
最好的事物,
您需要為數組“端口”提供一個維度。 關於 struct nav * port 的錯誤消息,這是 C++ 如何將數組衰減為指針的副作用
或者,由於您詢問是否有辦法不對維度進行硬編碼,只需使用 std::vector 即可。 您會發現使用 std::vector 通常既安全又高效。
“索引超出范圍”的另一個問題,我不能 100% 確定沒有看到 sample.csv 文件的內容,但如果文件只包含一個條目,那么索引“1”將超出范圍。 在 C++ C 樣式數組和 C++ std::vectorS 中,使用從零開始的索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.