passing an object through functions

I have a object called Shopper that i want to pass through an other function, but i didn't find how todo it.
I called the function HardwareStore::inputShopper() to input the name of the new shopper, and I am trying to pass it to an other function from the same cpp file. just to check if it's working.

The Error messages i get is this one:
1
2
3
4
5
6
HardwareStore.cpp:24:16: error: redefinition of 'newShopper' with a different type : 'HardwareStore' vs 'Shopper::Shopper' HardwareStore(newShopper);

HardwareStore.cpp:23:19: error: previous definition is here 
Shopper::Shopper newShopper(name,lname);

1error generated.


MainProgram.cpp
1
2
HardwareStore hws(10);
hws.inputShopper();


HardwareStore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
HardwareStore::HardwareStore(int numRegisters):numRegisters(numRegisters)
{
	for(int i=0; i<numRegisters; i++)
		registerQueue.push_back(queue<Shopper>());
		
		cout<<"This Worked"<<endl;
}
void HardwareStore::inputShopper(){
	string fname, lname;
	
	cout<<"First name : ";
		getline(cin, fname);
	cout<<"Last Name  : ";
		getline(cin, lname);
	Shopper::Shopper newShopper(fname,lname);
	HardwareStore::addShopperToLine(newShopper);	
}
void HardwareStore::addShopperToLine(const Shopper& shopper){
	
		cout<<"if it displays, it means it s Working";
}


Shopper.cpp
1
2
3
4
5
6
7
8
9
10
Shopper::Shopper(const string& firstName, const string& lastName):firstName(firstName),lastName(lastName){
		ShoppingCart newChoppingList;
}

string Shopper::getFname(){
	return firstName;
}
string Shopper::getLname(){
	return lastName;
}
Last edited on
Look at line 16 in HardwareStore.cpp:

HardwareStore(newShopper);

Here, you're invoking a constructor of HardwareStore, that takes a Shopper object as an argument. However, there is no such constructor; the only constructor you've defined takes an int not a Shopper:

1
2
3
4
HardwareStore::HardwareStore(int numRegisters):numRegisters(numRegisters)
{
//...
}


Even if you did have such a constructor, that line would achieve nothing except to create a temporary, unnamed HardwareStore object that immediately disappears again.

I suspect that what you really want is to call HardwareStore::addShopperToLine() passing in the new Shopper object. Note, however, that you've defined newShopper as a local variable, which means that when HardwareStore::inputShopper() ends, newShopper will go out of scope and be destroyed.
Last edited on
Sorry, thats what i wanted to do. i just updated it. just Copy and Past mistake.
I am planned to add the new Shopper to a queue of Shopper later in the next function "addShopperToLine()".
However, when i compile the code As it is, i get an Undefined symbol for architecture.
1
2
3
4
5
Undefined symbols for architecture x86_64:
  "Shopper::Shopper(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      HardwareStore::inputShopper() in HardwareStore-78e5ad.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Last edited on
Topic archived. No new replies allowed.