Advertisement

unresolved external symbol error with vectors.....

Started by November 30, 2002 09:42 PM
4 comments, last by OOBradm 21 years, 11 months ago
I have this in my common.h header file: #include "StdAfx.h" extern int TotalNumHourly,TotalNumCommission,TotalNumSalary; extern double TotalPayHourly,TotalPayCommission,TotalPaySalary; struct DataRecord { bool Hourly,Commission,Salary; char LastName [100]; char FirstName [100]; char Address [100]; char City [100]; char State [25]; char Zip [25]; char sex [25]; char SorM [25]; char YofS [25]; char Ssn [25]; char Age [25]; double STotalPay, SBonus, SSalary; double HHoursWorked, HHourlyRate, HRegularPay; double Crate,CItemsSold,CPricePerItem,CTotal; }; extern std::vector Vdata; then in a later function I tried to use that vector(AddEmp.cpp): else { DataRecord * ptr = new DataRecord; strcpy(ptr->LastName,cap_lname); strcpy(ptr->FirstName,cap_fname); strcpy(ptr->City,cap_City); strcpy(ptr->State,cap_State); strcpy(ptr->Address,cap_Address); strcpy(ptr->Zip,cap_Zip); strcpy(ptr->Age,cap_Age); strcpy(ptr->sex,cap_Sex); strcpy(ptr->SorM,cap_Mos); strcpy(ptr->YofS,cap_Ywc); Vdata.push_back(ptr); CDialog::OnOK(); } If I declare the vector inside AddEmp.cpp I get no errors, but when I declare it in common.h as I have posted above I get this error: unresolved external symbol "class std::vector > Vdata" (?Vdata@@3V?$vector@PAUDataRecord@@V?$allocator@PAUDataRecord@@@std@@@std@@A) any ideas of what I can do?
Look up the semantics for extern.

You still have to define your variable in a (single) source file.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Why not just turn your struct into a class, and create a Copy function or overload the = operator?
quote: Original post by Fruny
Look up the semantics for extern.

You still have to define your variable in a (single) source file.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

I looked up extern at msdn, it told me nothing that I dont already know... In order to declare my vector in a single source file I should write something in common.ccp, correct? What would I write? the variable is already declared and I dont need to preset any values..

thanks for your help guys, it is very much appreciated


quote: Original post by OOBradm
In order to declare my vector in a single source file I should write something in common.ccp, correct? What would I write? the variable is already declared and I dont need to preset any values..


The variable is declared, but not defined. It''s just like giving a function prototype but not the function. You still have to define the variable, like any other global variable in common.cpp. The extern declaration only affects the visibility of the variable, not its existence.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by Fruny
The variable is declared, but not defined. It''s just like giving a function prototype but not the function. You still have to define the variable, like any other global variable in common.cpp. The extern declaration only affects the visibility of the variable, not its existence.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


Thanks a lot, I was still a little unsure of how externs work but now after reading that I completely understand. Thanks again,
-Brad

This topic is closed to new replies.

Advertisement