Custom C++ header error undefined reference to `ClientData::ClientData [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Separating class code into a header and cpp file
(8 answers)
Closed 2 months ago.
I am Starting learning C++ from scratch, and now I am trying to import this custom header to one of my cpp. Its a pretty basic class.
ClientData.h
#ifndef CLIENTDATA_H
#define CLIENTDATA_H
#include <string>
using namespace std;
class ClientData{
public:
ClientData(int=0, const string & = "", const string & = "", double = 0);
void setAccountNumber(int);
int getAccountNumber() const;
void setLastName(const string &);
string getLastName() const;
void setFirstName(const string &);
string getFirstName() const;
void setBalance(double);
double getBalance() const;
private:
int accountNumber;
char lastName[15];
char firstName[10];
double balance;
};
#endif
tutor.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
#include "ClientData.h"
using namespace std;
int main(){
ClientData blankClient;
return 0;
}
I get an error of: undefined reference to ClientData::ClientData(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, double)
For an extra info: I am using MinGW Compiler, and I also name the header ClientData.h placed under the include folder of MinGW Compiler directory. I run this code under sublime text.
What should I do to solve this? Thankyou

Related

C++ Apple Mach-O Linker Error/Symbols "::" Not Found for Architecture

I have the following header file (Student.h)
#ifndef STUDENT_H_
#define STUDENT_H_
#include <string>
using namespace std;
class Student {
int id;
string name;
string school;
public:
/*
* Default ctor
*/
Student();
/*
* Overloaded ctor
*/
Student(int id, string name, string school);
/*
* Compares two students by ID for equality
*/
bool operator == (const Student& other) const;
/*
* Compares two students by ID for ordering
*/
bool operator < (const Student& other) const;
};
#endif /* STUDENT_H_ */
And here is my implementation (in another file called Student.cpp):
#include "Student.h"
#include <string>
using namespace std;
bool Student::operator==(const Student& other) const{
return(Student::id == other.id);
}
bool Student::operator<(const Student& other) const{
return(Student::id < other.id);
}
And then finally a main.cpp file which references both files:
#include "Student.h"
#include "Student.cpp"
#include <iostream>
using namespace std;
int main() {
bool equal;
Student s1(111, "Jeff", "Rockywood High");
Student s2(100, "Bobby", "Carmel High");
equal = (s1==s2);
cout << equal;
}
I am getting an error from xcode telling me that:
the Undefined symbols for architecture x86_64:
Student::Student(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
By placing #include "Student.cpp" in the Student.cpp file I had included the same file twice, without realizing it. Thus, Student::Student was defined twice...and thus the duplicate symbol error.

Class inheritance: Constructor and member functions of class not recognized by compiler

I just ran into trouble with my code of class inheritance: the constructors are not recognized by the compiler. Neither are any member functions.
For example, if I call the constructor,
my testfile(test.cpp) starts like this:
#include "salariedemployee.h"//This is a class inherited from 'employee.h', the base class
#include "Administrator.h"//This is a class inherited from "salariedemployee.h"
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using namespace employeessavitch;
int main()
{
Employee boss("Mr Big Shot","987-65-4321");//I try to call constructor in the base class "employee";
}
The compiler gives an error like
undefined reference to `employeessavitch::Employee::Employee(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
If I try to call the constructors in inherited class such as
SalariedEmployee boss("Mr Big Shot","987-65-4321",10500.50);//a constructor about name, SSN number, and salary
it gives an error like:
undefined reference to `employeessavitch::SalariedEmployee::SalariedEmployee(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double)'
I would like to know what is going wrong?
My base class header file is written as :
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
namespace employeessavitch
{
Employee( );//default constructor
Employee(string the_name, string the_ssn); constructor about name and ssn
}#endif
My inherited class header file is written as:
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H
#include <string>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
class SalariedEmployee : public Employee
{
public:
SalariedEmployee( );//default constructor
SalariedEmployee (string the_name, string the_ssn, double the_weekly_salary);//constructor about name, ssn and salary
//Other member function and variables are ommitted here.
}#endif
I am pretty sure that the namespace is all right as I can write std cin and cout.
My cpp file of implementation was like:
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
Employee::Employee( ) : name("No name yet"), ssn("No number yet"), net_pay(0)
{
//deliberately empty
}
}
and
#include <iostream>
#include <string>
#include "salariedemployee.h"
using namespace std;
namespace employeessavitch
{
SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)
{
//deliberately empty
}
SalariedEmployee::SalariedEmployee(string the_name, string the_number, double the_weekly_salary): Employee(the_name, the_number), salary(the_weekly_salary)
{
//deliberately empty
}
}
I believe the Employee constructors are not declared as part of the class definition.
Try this in employee.h:
namespace employeessavitch
{
class Employee
{
public:
Employee( );//default constructor
Employee(string the_name, string the_ssn); //constructor about name and ssn
};
}
The code below should work (do this for your other class also):
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
namespace employeessavitch
{
Employee( )
{
}//default constructor
Employee(string the_name, string the_ssn)// constructor about name and ssn
{
}
}#endif
Alternatively, if you believe that you already have an implementation defined in your .cpp file then check to make sure that the parameters match. The cpp implementation should look like:
employeessavitch::Employee::Employee()
{
}
employeessavitch::Employee::Employee(string the_name, string the_ssn)
{
}
Also, it is best not to have a 'using namespace' statement in your header file. The reason is that this means that any code that includes this header file is automatically using the namespace which sort of defeats the purpose. If you don't want to have a cpp file then use the 'std::' prefix instead of 'using namespace std' in your header file.
I wasn't quite sure from the question whether or not you have a separate .cpp file that includes the constructor implementations. If you do, posting it would be helpful in trying to assist with your problem.
You have to add the definition of both cstors to your employee.cpp file.
This one wasn't defined:
Employee(); //cstor
Employee( string the_name, string the_ssn); // <-- This one isn't defined
Add both cstor definitions to your file and it compiles:
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
Employee::Employee( ) : name("No name yet"), ssn("No number yet"), net_pay(0)
{
//deliberately empty
}
Employee::Employee( string the_name, string the_ssn) // <-- is missing in
{ // your cpp file
//deliberately empty
}
}
Undefined reference simply means that even though it is getting the function definitions (i.e. from header file), it is not getting the implementation for the functions (SalariedEmployee and Employee). Did you implement the function in the .cpp file for Employee(string the_name, string the_ssn);?

C++ project setup, multiple definitions

I'm working on my first large C++ project, and I'm having huge problems getting it to build corectly.
first of all, the errors:
/tmp/ccn7hjru.o: In function `match(std::basic_istream<char, std::char_traits<char> >&, char const*)':
Geometry.cpp:(.text+0x0): multiple definition of `match(std::basic_istream<char, std::char_traits<char> >&, char const*)'
/tmp/ccfuS3Jb.o:Camera.cpp:(.text+0x0): first defined here
/tmp/ccn7hjru.o: In function `eat(std::basic_istream<char, std::char_traits<char> >&)':
Geometry.cpp:(.text+0xda): multiple definition of `eat(std::basic_istream<char, std::char_traits<char> >&)'
/tmp/ccfuS3Jb.o:Camera.cpp:(.text+0xda): first defined here
/tmp/ccIOhdcQ.o: In function `match(std::basic_istream<char, std::char_traits<char> >&, char const*)':
Light.cpp:(.text+0x0): multiple definition of `match(std::basic_istream<char, std::char_traits<char> >&, char const*)'
/tmp/ccfuS3Jb.o:Camera.cpp:(.text+0x0): first defined here
/tmp/ccIOhdcQ.o: In function `eat(std::basic_istream<char, std::char_traits<char> >&)':
Light.cpp:(.text+0xda): multiple definition of `eat(std::basic_istream<char, std::char_traits<char> >&)'
/tmp/ccfuS3Jb.o:Camera.cpp:(.text+0xda): first defined here
....
this goes on for hundreds of lines
All of my c++ course files look like this:
#include "Camera.h"
#include "util.h"
Camera::Camera() {
// TODO Auto-generated constructor stub
}
Camera::Camera(int x, int y) {
this->resX = x;
this->resY = y;
}
Camera::~Camera() {
// TODO Auto-generated destructor stub
}
...more class methods below...
and the header files all look like this:
#ifndef CAMERA_H_
#define CAMERA_H_
#include "SceneElement.h"
#include "P3D.h"
#include "Ray.h"
#define CAMERA_PRE "{CAM:"
#define CAMERA_POST ":CAM}"
#define TAG_LOCATION "LOC:"
#define TAG_PLANE "PLANE:"
#define TAG_UPPER_RIGHT "UR:"
#define TAG_UPPER_LEFT "UL:"
#define TAG_LOWER_RIGHT "LR:"
#define TAG_LOWER_LEFT "LL:"
#define TAG_RES_X "RESX:"
#define TAG_RES_Y "RESY:"
class Camera: public SceneElement {
public:
P3D location;
P3D upperLeft;
P3D upperRight;
P3D lowerLeft;
P3D lowerRight;
int resX, resY;
Camera();
Camera(int, int);
virtual ~Camera();
virtual void toStream(std::ostream &);
virtual void fromStream(std::istream &);
Ray getRay(int, int);
};
#endif /* CAMERA_H_ */
the one exception is a util file, which looks like this:
#include "util.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
void match(std::istream &str, const char* expected){
int len = strlen(expected);
char* fromStream = (char*)malloc(len+1);
str.read(fromStream, len);
fromStream[len] = 0;
if(strcmp(fromStream, expected)){
std::cout << "expected " << expected << ", got " << fromStream << "\n";
free(fromStream);
exit(1);
}
free(fromStream);
}
void eat(std::istream &str){
char c;
while(c=str.peek(), c == ' ' || c == '\n' || c == '\t'){
str.get();
}
}
with a header file that looks like this:
#ifndef UTILS
#define UTILS
#include <iostream>
void match(std::istream &str, const char* expected);
void eat(std::istream &str);
#endif
Look into SceneElement.h, P3D.h and Ray.h. Most likely, you have included util.cpp in one of these header files.
Another possibility could be a definition of match() and eat() in one of the header files.

C++ Simple compile error

I am having a Compile issue.
I have one Class
I have one header file
And of course Main to test my work.
But I am getting compile error, it is out of my understanding what I am doing wrong.
Header File:
#ifndef AGENT_H
#define AGENT_H
using namespace std;
class Agent
{
public:
Agent(string);
virtual ~Agent();
private:
string name;
};
#endif /* AGENT_H */
Agent Class (Agent.cpp)
#include "Agent.h"
using namespace std;
Agent::Agent(string _name)
{
this->name = _name;
}
Agent::~Agent()
{
delete this->name;
}
And my Main:
#include <cstdlib>
#include <iostream>
#include "Agent.h"
using namespace std;
int main(int argc, char** argv)
{
Agent agent1("Danila");
return 0;
}
So I am getting such strange error:
undefined reference to `Agent::Agent(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/main.cpp:17: undefined reference to `Agent::~Agent()'
/main.cpp:17: undefined reference to `Agent::~Agent()'
Could you guys help me understand whats wrong there?
You need an #include <string> in your header file.
Also, for good practice, keep the using namespaces in your .cpp files, if any.
You compiled without telling the compiler about Agent.cpp. I.e. you need something like this, for g++:
$ g++ main.cpp Agent.cpp -o myprogram

class method error in c++

I am getting an error when I declare a class:
#include <iostream>
#include "testing/test.h"
#include <string>
using namespace std;
int main(void)
{
test links;
string content="this is an string";
links.getcont(content);
}
test.h
#ifndef TEST_H_
#define TEST_H_
#include<string>
using namespace std;
class test {
public:
string getcont(string content);
};
#endif /* TEST_H_ */
test.cpp
#include "test.h"
#include <iostream>
using namespace std;
string getcont(string content)
{
cout << content;
return content;
}
When I run this I get this error:
undefined reference to test::getcont(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
Well , in your test.cpp file replace the getcont function for this
string test::getcont(string content){ //code here; }
The problem is that you are not saying that getcont is a member function of the test class.
Also, consider making it a const function and passing a const string reference
string
test::getcont( const string& content) const
{
return content;
}