GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-May-2005, 01:33
Clive73 Clive73 is offline
New Member
 
Join Date: May 2005
Location: Bristol
Posts: 9
Clive73 is on a distinguished road

Trouble passing STL into a function


Hi!

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:
include <vector>

class people
{
   code here...
};

int main()
{
  vector<people> thePeople;
  people *me=new people();
  thePeople.push_back(*me);


  return 0;
}


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  
Old 10-May-2005, 01:50
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
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:
#include <vector>
using std::vector;

class people
{
public:
	void PrintMe()
	{
		std::cout << "Hi! Here I am!" << std::endl;
	}
};

void thisFunction(vector<people> &);

int main()
{
	vector<people> thePeople;
	people * me = new people();
	thePeople.push_back(*me);
	people * you  = new people();
	thePeople.push_back(*you);
	thisFunction(thePeople);

	return 0;
}

void thisFunction(vector<people> & vect)
{
	
	for (size_t i = 0; i < vect.size(); i++)
	{
		vect[i].PrintMe();
	}
}

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  
Old 10-May-2005, 14:41
Clive73 Clive73 is offline
New Member
 
Join Date: May 2005
Location: Bristol
Posts: 9
Clive73 is on a distinguished road
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:
#include <vector>
#include "passingvector.h"
#include "passingother.h"
using std::vector;
int main()
{
	vector<people> thePeople;
	people *me = new people();
	thePeople.push_back(*me);
	people *you = new people();
	thePeople.push_back(*you);
	thisFunction(thePeople);

	return 0;
}

passingvector.h:-
CPP / C++ / C Code:
#include <iostream>

#ifndef PASSINGVECTOR_H
#define PASSINGVECTOR_H

class people
{
public:
	void printMe()
	{
		std::cout << "Hi! Here I am!" << std::endl;
	}
};

#endif

passingother.cpp
CPP / C++ / C Code:
#include "passingother.h"

void thisFunction(vector<people> &theFolk)
{
	for (size_t i = 0; i<theFolk.size();i++)
	{
		theFolk[i].printMe();
	}
}

passingother.h
CPP / C++ / C Code:
#include "passingvector.h"
#include <vector>
using std::vector;

void thisFunction(vector<people> &);

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  
Old 10-May-2005, 16:59
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by Clive73
Compiles fine but upon linking gives the error:
Code:
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?$allocator@Vpeople@@@std@@@std@@@Z) Debug/passingvector.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe.
If I compile (g++ cygwin version) with the following line:
Code:
g++ -Wall passingvector.cpp -s -o Main
I get the following error:
Code:
/tmp/ccZz4Vmw.o(.text+0x20c):passingvector.cpp: undefined reference to `thisFunction(std::vector<people, std::allocator<people> >&)' collect2: ld returned 1 exit status
The problem is that you have to add all the source files on the command line. You have the definition of thisFunction in the header but no actual code as far as the compiler is concerned.
Using the command line:
Code:
g++ -Wall passingvector.cpp passingother.cpp -s -o Main
should take care of the problem.

Quote:
Originally Posted by Clive73
Also, whats the difference between using using namespace std; and using std::vector ?

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  
Old 10-May-2005, 17:44
Clive73 Clive73 is offline
New Member
 
Join Date: May 2005
Location: Bristol
Posts: 9
Clive73 is on a distinguished road
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  
Old 10-May-2005, 18:00
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by Clive73
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.

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 GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 05:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.