2015-10-29 74 views
-1

當前正在將我的程序中的一個從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 typeport聲明。

最重要的是:有沒有一種方法不硬編碼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:所述頭應該被解讀級一致性的目的。

enter image description here

最佳,

+1

'nav port [];'< - 這是什麼? – Drop

+0

可能幫助:[The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Drop

+1

你不能定義一個「空」陣列。如果你想添加運行時然後使用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)。 –

回答

0

您需要提供一個尺寸爲陣 「端口」。關於帶有struct nav * port的錯誤消息,這是C++如何將數組衰減爲指針的副作用

或者,由於您詢問是否有方法不對硬件維進行編碼,只需使用std :: vector 。你會發現使用std :: vector通常既安全又有效。

「索引超出範圍」的另一個問題是,我不能100%確定沒有看到sample.csv文件的內容,但是如果文件只包含一個條目,那麼索引「1」將超出範圍。在C++中,C風格的數組和C++ std :: vectorS使用基於零的索引。

+0

感謝您的回覆。關於sample.csv的內容,我用照片編輯了這篇文章。希望能幫助到你。乾杯。 – owner

相關問題