![]() |
|
#1
|
|||
|
|||
Trouble passing STL into a functionHi!
I'm new to c++ and STLs and am experiencing problems passing an STL into a function. I can create an STL fine, and do everything with it, but not how to pass it into a function. CPP / C++ / C Code:
I can understand how to pass a single STL class object into a function but not the entire vector list. The problem I seem to be having is how to define it in the .h file. To define a single class object it works with: void thisFunction(people const &) however, when I attempt to define void thisFunction(vector<people> &) it doesn't like the <>. I think I've included all the relevant files. I'm clearing missing something important in the declarations, and all the examples I've found don't deal with STL's and classes. Hope this helps, and thanks for any help! Clive. Last edited by LuciWiz : 10-May-2005 at 01:40.
Reason: Please use / for the end tag, e.g. [c++] //code [/c++]
|
|
#2
|
||||
|
||||
|
Did you include the header of the class people in the one where you define the function? Did you use the std namespace and include the <vector> header in this header? (although if you did include the other header there is no more need for this). The following works:
CPP / C++ / C Code:
Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
|||
|
|||
|
Many thanks for that as it worked fine, however I'm experiecing the same errors as I did before when I attempt to split it up into separate files.
passingvector.cpp contains: CPP / C++ / C Code:
passingvector.h:- CPP / C++ / C Code:
passingother.cpp CPP / C++ / C Code:
passingother.h CPP / C++ / C Code:
Compiles fine but upon linking gives the error: Compiling... passingvector.cpp Linking... passingvector.obj : error LNK2001: unresolved external symbol "void __cdecl thisFunction(class std::vector<class people,class std::allocator<class people> > &)" (?thisFunction@@YAXAAV?$vector@Vpeople@@V?$allocat or@Vpeople@@@std@@@std@@@Z) Debug/passingvector.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. I've tried moving the includes around (adding and taking them away) but it seems to make no difference. I first assumed that not all the files were correctly being linked and started including cpp files from the h until someone told me this shouldn't be done. I'm clearly doing something obvious wrong and my lack of experience isn't allowing me to figure it out. Also, whats the difference between using using namespace std; and using std::vector ? Thanks, Clive. Last edited by LuciWiz : 10-May-2005 at 15:44.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#4
|
||||
|
||||
|
Quote:
Code:
Code:
Using the command line: Code:
Quote:
When you go with using namespace std; you include everything in the standard namespace. If instead you code it as using std::vector; you only get precisely what you ask for. I personally prefer the using std:: method instead of using the scope resolution directly in the code like you did in passingvector.h but they act identically. Avoidence of the using namespace std; will at the very least, serve as a reminder to you of where everything came from when you use std:: or using std::. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#5
|
|||
|
|||
|
Fantastic - that worked. Thanks very much. In the main file I included the cpp file of the other instead of the h. The reason I did that originally is I was told that you should include the h files (which contain a definition of the function) and the cpp file (which gives the entire function) includes it's own h file (if all this makes sense!). The reasoning was that as long as there's a link between each file then it "should" work fine.
But thanks anyway. Clive. |
|
#6
|
||||
|
||||
|
Quote:
Your welcome. Just remember that the source goes on the command line and the headers get included with #include and you won't have that problem again. Most likely some other complicated pulling your hair out problem, just not that one. __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| [Tutorial] Function Pointers | aaroncohn | C++ Forum | 4 | 17-Feb-2006 11:33 |
| Passing a C++ vector to function | SnackMan78 | C++ Forum | 7 | 18-Apr-2005 12:29 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 04:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The