Passing an object through a function on separate file

I am having real trouble trying to make this code working.
App.cpp (main file)
1
2
3
4
5
6
7
8
9
10
11
12
#include "HardwareStore.h"
#include <iostream>

using namespace std;

int main(){
	cout<<"Start"<<endl;
	HardwareStore hws(10);
	hws.inputShopper();

	return 0;
}

HardwareStore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef HARDWARESTORE_H
#define HARDWARESTORE_H
#include "Shopper.h"
#include <iostream>
#include <queue>
#include <list>
using namespace std;
class HardwareStore{
	public:
		HardwareStore(int numRegisters);
		void inputShopper();
		void addShopperToLine(const Shopper& shopper);
	private:
		int numRegisters;
		list<Shopper>listShopper;
		list<queue<Shopper> > registerQueue;	
};
#endif 

HardwareStore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "HardwareStore.h"
HardwareStore::HardwareStore(int numRegisters):numRegisters(numRegisters){
    for(int i=0; i<numRegisters; i++)
        registerQueue.push_back(queue<Shopper>());
}
void HardwareStore::inputShopper(){
	string fname, lname;
	cout<<"First name: ";
		getline(cin,fname);
	cout<<"Last Name : ";
		getline(cin,lname);	
	Shopper::Shopper newShopper(fname,lname);
	HardwareStore::listShopper(newShopper);
}
void HardwareStore::addShopperToLine(const Shopper& shopper){
	//Insert the last shopper added(top of the list) to the queue. 
}

Shopper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef SHOPPER_H
#define SHOPPER_H
#include <iostream>
#include <list>
using namespace std;
class Shopper{
	public: 
		Shopper(const string& firstName, const string& lastName);
		string getFname();
		string getLname();
	private:
		string firstName;
		string lastName;
};
#endif 

Shopper.cpp
1
2
3
4
5
6
7
8
9
10
#include "Shopper.h"
Shopper::Shopper(const string& firstName, const string& lastName):firstName(firstName),lastName(lastName){
	cout<<"New Shopper Created successfully "<<endl;
}
string Shopper::getFname(){
	return firstName;
}
string Shopper::getLname(){
	return lastName;
}
HardwareStore.cpp,line 13, HardwareStore::listShopper(newShopper);
is error
it should be this->listShopper.push_back(newShopper);
it s the same problem .
always get the same error :
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-406885.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I use MinGW, and can compile and link your code successfully after the correct in line 13 of HardwareStore.cpp.
I think maybe HardwareStore.cpp,line 12 should change to Shopper newShopper(fname,lname);
I tried that already but it doesn't work.
I just compile the program on Mac's terminal.
Maybe is the way i am compiling the project. is it possible to check if it's correct>
1
2
3
4
:> g++ -c Shopper.cpp
:> g++ -c HardwareStore.cpp
:> g++ -c App.cpp
:> g++ -o App App.cpp HardwareStore.cpp 


yes. last command should be g++ -o App Shopper.o HardwareStore.o App.o
It is working ! thank you .
By the way. if a project contains 50 cpp files, will have to write a line with all the .o files coming from it to compile it?
if a project contains 50 cpp files, will have to write a line with all the .o files coming from it to compile it?

If you're going to insist on using the command line, yes. Or else you can use a series of command lines to build libraries that you then link together, but that's not really any less work overall.

This is why no-one sane uses the command-line to compile anything other than the smallest of projects. This is why we have Makefiles, and IDE's, and CMake, and any number of other build systems that are out there.
fare enough !

Just a small update.
If there is lot of files to be compiled, you can always use the "makefile" option on Linux, Unix or mac. I don't know if it works on Microsoft Ms-Dos, but, it s much better with "make" command . no need of an IDE

thank you for the help
Last edited on
Topic archived. No new replies allowed.