linking hpp and cpp issues with g++ compiler - c++

I am relatively new to c++ and have a solid C and Object Oriented background. So I have the following code written.
I have Person.hpp which is as follows:
class Person
{
private:
struct h
{
char *name;
unsigned char age;
};
struct h entities;
public:
unsigned char age();
char *name();
Person(char *name, unsigned char a);
~Person();
};
The Person.cpp looks as follows:
#include "Person.hpp"
char *Person::name()
{
return entities.name;
}
unsigned char Person::age()
{
return entities.age;
}
Person::Person(char *name, unsigned char a)
{
entities.name = name;
entities.age = a;
}
And finally main.cpp looks like the following:
#include <iostream>
#include "Person.hpp"
int main()
{
Person someone("something", 100);
printf("%s is %d old\n", someone.name(), someone.age());
return 0;
}
I am already aware of the warning that it will cause due to the fact that the string is not constant. This will not cause an error when trying to compile. I tried compiling the code in two different methods. One by just compiling all together which would look like the following:
g++ -o main main.cpp Person.cpp
And the second method I tried was to compile them into object files and then link them together:
g++ -c main.cpp
g++ -c Person.cpp
g++ -o main main.o Person.o
They both give the following error:
/usr/bin/ld: main.o: in function `main':
main.cpp:(.text+0x5b): undefined reference to `Person::~Person()'
/usr/bin/ld: main.cpp:(.text+0x6e): undefined reference to `Person::~Person()'
collect2: error: ld returned 1 exit status
Any help would be greatly appreciated.

You have a destructor declared here in your header file:
public:
unsigned char age();
char *name();
Person(char *name, unsigned char a);
~Person(); // <===== Declared here
But you haven't provided any definitions for it.
However, the destructor is called after your main function returns (you can learn more about this behavior here), so it needs a definition.
You can make the compiler generate the definition for you by either omitting the destructor declaration or use ~Person() = default.

The problem is that you declare both an constructor and destructor in Person.hpp, but only define the constructor in the Person.cpp file.
Try add the following code to Person.cpp:
Person::~Person()
{
}

Related

How to inherit from an abstract class properly in C++?

I couldn't find a proper topic for this question as I haven't got a proper error message.
I'm trying to create a management system for a restaurant which mainly provides pizza as well as other foods(pasta, wings, etc). I want this system to be used by the staff. I have created an abstract class named Foods that can be used to inherit by other foods. So far I have created a class that inherits from Foods named Pizza. Below are my code.
PS: I have used namespaces for organize foods and staff members separately. As far as I know some people doesn't recommend namespace and my apologies if you're one of them.
interfaces.h
#include <vector>
#include <string>
namespace foods{
class Food{
double price;
// since the sauces and drinks are given with foods.
static const std::vector<std::string> sauces;
static const std::vector<std::string> drinks;
public:
virtual int retPrice() = 0;
virtual void ask() = 0; // ask user what to add
virtual ~Food() = default;
};
const std::vector<std::string> Food::sauces = {"Blue cheese", "Garlic", "Honey BBQ", "Marinara"};
const std::vector<std::string> Food::drinks = {"Pepsi", "Mountain Dew", "Coca Cola"};
class Pizza: public Food{
const double price;
const std::string pizzaType; // whether it is chicken, beef, etc.
const std::string size; // small, medium or large
int crust = 1; // how crust it is from 1-5
std::vector<std::string> toppings; // to store toppings
public:
Pizza(): price(15), pizzaType(" "), size(" "){}
int retPrice() override; // the price should change according to the type
void ask() override; // ask the customer for a pizza
void createACustom(); // create a custom pizza with desired toppings
};
};
functions.cpp
#include <iostream>
#include "interfaces.h"
namespace foods{
int Pizza::retPrice(){
return (price+5);
}
void Pizza::ask(){
std::cout << "Hello World!";
}
}
test.cpp
#include "interfaces.h"
int main(){
foods::Pizza* pizza = new foods::Pizza();
}
And I'm getting following error.
/usr/bin/ld: /tmp/ccQRR5B8.o: warning: relocation against `_ZTVN5foods5PizzaE' in read-only section `.text._ZN5foods5PizzaC2Ev[_ZN5foods5PizzaC5Ev]'
/usr/bin/ld: /tmp/ccQRR5B8.o: in function `foods::Pizza::Pizza()':
test.cpp:(.text._ZN5foods5PizzaC2Ev[_ZN5foods5PizzaC5Ev]+0x2b): undefined reference to `vtable for foods::Pizza'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
I tried using the keyword override and also made a default deconstructor, yet nothing seems working. I want to know what this error message means and a solution for this. In addition to that what is vtable?
Appreciate your time and answers.
EDIT 1
I have compiled it with g++ -Wall -Wextra test.cpp functions.cpp -o test, which is wrong and then I did g++ -Wall -Wextra test.cpp functions.cpp -o test and I'm getting following error.
/usr/bin/ld: /tmp/ccmv2G17.o:(.bss+0x0): multiple definition of `foods::Food::sauces[abi:cxx11]'; /tmp/ccuBNQjX.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccmv2G17.o:(.bss+0x20): multiple definition of `foods::Food::drinks[abi:cxx11]'; /tmp/ccuBNQjX.o:(.bss+0x20): first defined here
collect2: error: ld returned 1 exit status
Why is it saying that it has multiple definitions?
You need to implement the static member variables sauces and drinks in functions.cpp and not in interfaces.h.
functions.cpp
namespace foods {
int Pizza::retPrice() {
return (price + 5);
}
void Pizza::ask() {
std::cout << "Hello World!";
}
// implement the static variables here.
const std::vector<std::string> Food::sauces = { "Blue cheese", "Garlic", "Honey BBQ", "Marinara" };
const std::vector<std::string> Food::drinks = { "Pepsi", "Mountain Dew", "Coca Cola" };
}
And remove them from interfaces.h.
If you implement them in interfaces.h they end up being implemented in each .cpp file that includes interfaces.h.
It's basically the same problem as if you define a global variable in a .h file.

"Undefined Refrence to Foo" while compiling with G++

I have three files:
my.cpp
#include "my.h"
#include <iostream>
void print_foo() {
cout << foo << '\n';
}
void print(int i) {
cout << i << '\n';
}
my.h
extern int foo;
void print_foo();
void print(int);
use.cpp
#include "my.h"
int main(int argc, char *argv[]) {
foo = 7;
print_foo();
print(99);
return 0;
}
Now when I run g++ my.cpp use.cpp I get the error
/usr/bin/ld: /tmp/ccUKJUlZ.o: in function `print_foo()':
my.cpp:(.text+0x6): undefined reference to `foo'
/usr/bin/ld: /tmp/ccN0mIhY.o: in function `main':
use.cpp:(.text+0x11): undefined reference to `foo'
collect2: error: ld returned 1 exit status
Additionally, if I run g++ -c my.cpp everything goes alright, but, if I then run g++ my.o use.cpp I get the same error.
You never actually define a variable foo - in both use.cpp and my.cpp, you use foo, and in my.h you declare it as an extern.
See the beginning of this response for more information on declaring vs. defining. You may think that your problem would be solved if you added a type in front of your foo = 7 line in use.cpp; however, what you also need to do is make foo a global variable instead of a local one (which it is when you declare it simply within main), as extern will only "find" variables that have global scope. You can make a variable global by declaring it outside of any function (side note - you should only use global variables when you absolutely have to).
Therefore, you could solve your problem by changing your use.cpp to the following:
#include "my.h"
int foo = 7;
int main(int argc, char *argv[]) {
print_foo();
print(99);
return 0;
}

g++ error: undefined reference while netbeans compiles in linux [duplicate]

This question already has answers here:
"undefined reference to" using 'g++' to compile a C++ program
(3 answers)
Closed 9 years ago.
Im using NetBeans IDE in linux mint for C++ programming. NetBeans compiles the code just fine but when i try to compile the main.cpp using g++ as in g++ main.cpp i get undefined reference errors:
I know there are many questions like this one and they the problem can be fixed by using the right -l library but I couldn't find any related to NetBeans/g++ and I couldn't figure how to overcome this problem.
Any help is appreciated.
Errors:
tmp/ccXSo5xI.o: In function `main':
main.cpp:(.text+0x82): undefined reference to `Hospital::Hospital(int, int, int)'
main.cpp:(.text+0xf3): undefined reference to `Resident::Resident(int, int)'
.
.
.
main.cpp
#include <iostream>
#include <cstdio>
#include "Resident.h"
#include "Hospital.h"
#include <stack>
using namespace std;
void readFromFiles(int [][10], int [][10]);
void readFromFiles(Hospital*[10], Resident*[10]);
void print10(int [][10], int [][10]);
void print10(Hospital*[10], Resident*[10]);
int main(void) {
int hospital[10][10] = {0};
int resident[10][10] = {0};
Hospital **hospitals = new Hospital*[10];
for(int i = 0 ; i < 10 ; i++)
hospitals[i] = new Hospital(3, i, 10); //3: quota, i: hospital number, 10:prefereneceLength
Resident **residents = new Resident*[10];
for(int i = 0 ; i < 10 ; i++)
residents[i] = new Resident(i,10); //i: hospital number, 10:prefereneceLength
.
.
.
Hospital.h
#ifndef HOSPITAL_H
#define HOSPITAL_H
#include "Resident.h"
using namespace std;
class Hospital {
public:
//constructors & destructors
Hospital();
Hospital(const int, const int, const int);
Hospital(const Hospital& orig);
virtual ~Hospital();
//getters & setters
int getNumber();
int getQuota();
int** getPreferenceList();
//member functions
void addPreference(const int, const int);
private:
int number;
int* preferenceList[2]; //1st row: preference order ; 2nd row: admission status
int quota;
Resident *admittedResidents;
};
#endif /* HOSPITAL_H */
Resident.h
#ifndef RESIDENT_H
#define RESIDENT_H
class Resident {
public:
//constructors & destructors
Resident();
Resident(const int, const int);
Resident(const Resident& orig);
virtual ~Resident();
//getters, setters
int getNumber();
int* getPreferenceList();
bool getAdmissionStatus();
//member functions
void addPreference(const int, const int);
private:
int number; //resident number
int proposalCount;
int* preferenceList; //not inverse, unlike Hospitals pref list
bool admissionStatus;
};
#endif /* RESIDENT_H */
I know that's a lot of code even though I shortened them, but I think they are the necessary parts. Thanks.
You need to include all source files:
g++ main.cpp hospital.cpp resident.cpp
g++ main.cpp will try to compile and link main.cpp into an executable (a.out). In the link stage, ld is not finding the symbols for the Hospital and Resident constructors while are called in main.cpp because they have not yet been compiled.
If you just want to compile main.cpp, use:
g++ -c main.cpp
You can link the object files later using:
g++ main.o hospital.o resident.o
If you want to compile and link the whole lot:
g++ main.cpp hospital.cpp resident.cpp

C++: Linking files with GCC compiler

I have three files : myh.h; my.cpp; use.cpp. Here are the contents of the files:
myh.h
extern int foo;
void print_foo();
void print(int);
my.cpp
#include "myh.h"
#include <iostream>
void print_foo()
{
std::cout<<foo<<std::endl;
}
void print(int i)
{
std::cout<<i<<std::endl;
}
use.cpp
#include "myh.h"
int main()
{
foo=7;
print_foo();
print(99);
return 0;
}
GCC spews out the following error:
my.o:my.cpp:(.text+0x7): undefined reference to `foo'
use.o:use.cpp:(.text+0x10): undefined reference to `foo'
collect2: ld returned 1 exit status
I compile the files using the -c command and it doesn't give errors. I link using the following command:
g++ -o final my.o use.o
What is the problem here, I read other topics with similar problems, and the case here is just strange .....
For the curious this is an exercise drill from Stroustrup's book Programming principles of using C++
Edit: I did as dasblinkenlight said, and in use.cpp I added an int in front of foo (so now foo is defined), but I still get this error:
my.o:my.cpp:(.text+0x7): undefined reference to `foo'
collect2: ld returned 1 exit status
Which tells me that it is not defined in my.cpp also? If I have to define it everywhere what is the point of including it in the header file, or how should this be approached more appropriately?
You get a linker error because you declared foo, but you never defined it.
extern int foo is only a declaration; it does not cause allocation of memory for the foo variable, only promises that you will do it at some other place. To fix it, you need to add this line to one of the cpp files, like this:
#include "myh.h"
int foo;
int main()
{
foo=7;
print_foo();
print(99);
return 0;
}
The problem is that foo is declared but not defined. You need to define foo in exactly one of the translation units, e.g.:
int foo = 0;

Nested class, undefined reference, static method

I have problem and no idea how to resolve it. I believe this is stupid trivial:
I have 3 files:
Util.hpp
class Util
{
public:
class BitParser
{
public:
static bool getBitAt(int buf, int idx);
};
};
Util.cpp
#include "Util.hpp"
bool Util::BitParser::getBitAt(int buf, int idx)
{
return true;
}
application.cpp
#include "Util.hpp"
int main(int argc, char* argv[])
{
Util::BitParser::getBitAt(1,1);
}
Of couse, files listed above are in the same directory. And now when I try to link and compile I recieve linker error:
$ g++ -o app application.cpp
application.cpp:(.text+0x19): undefined reference to `Util::BitParser::getBitAt(int, int)'
collect2: ld returned 1 exit status
What is screwed up?
You told g++ to compile your 'main' program, but didn't tell it about the Util module. Add Util.cpp to the command line and all should work well.
The compiler has brewn an "application.o" file that refers to the Util::bitparser functions.
The linker should 'link' these referrals to the "util.o" file, containing the actual code for these functions. But it has no .o file containing a function satisfying the link. That's what it calls "undefined reference": "application.o" refers to a function the linker doesn't find.
You need to compile (and link) all the .cpp files. So in your case, the command would be
$ g++ -o app application.cpp Util.cpp
Better still, write a Makefile to do this for you.
You have to include both application.cpp and Util.cpp in the build.