0

I have this annoying error; Undefined Reference to Shape::Shape(...), Shape::getName(...), Shape::getAge(...)

My Main.cpp is this

#include <iostream>
#include <string>
#include "Bit.h"

using namespace std;

int main()
{
    //simple assignment
    string name;
    int age;
    cout<<"enter name: ";
    cin>>name;
    cout<<"enter age: ";
    cin>>age;

    Shape sh(name,age); //class declaration (i think here is the problem)

    cout<<endl<<"name: "<<sh.getName();
    cout<<endl<<"age: "<<sh.getAge();




    return 0;
}

This is the Bit.h header

#include <iostream>
#include <string>

using namespace std;

#ifndef BIT_H
#define BIT_H

 //creating class
class Shape{
    string newName;
    int newAge;

public:
    //Default Constructor
    Shape();
    //Overload Constructor
    Shape(string n,int a);
    //Destructor
    ~Shape();
    //Accessor Functions
    string getName();
    int getAge();

};

And finally, this is the Bit.cpp

#include "Bit.h"
//constructors and destructor
Shape::Shape(){
    newName="";
    newAge=0;
}

Shape::Shape(string n, int a){
    newName=name;
    newAge=age;
}

Shape::~Shape(){

}

string Shape::getName(){
    return newName;
}
//getters
int Shape::getAge(){
    return newAge;
}

I understand, that this might be a very simple problem/error, but I have been struggling for about 2 hours. I suppose that the error is in the declaration od "sh" object, even if I declare it like this "Shape sh();" or "Shape sh;", there are still errors. Thanks

EDIT. GNU GCC Compiler EDIT2. Using Code Blocks (sorry for forgetting all these)

underscore_d
  • 6,309
  • 3
  • 38
  • 64

1 Answers1

2

You're probably not compiling Bit.cpp, but only Main.cpp.

Considering that Bit.h, Bit.cpp and Main.cpp are in the same folder, here is how you should compile it :

g++ *.cpp

It will still not compile, as in the default constructor, you're trying to initialize name, and age which both don't exist.

You probably meant newAge, and newName?

In the other Constructor, name and age also don't exist, you probably meant n, and a?

souki
  • 1,305
  • 4
  • 23
  • 39
  • That was a typo, indeed, still the same errors appear – Nikos Stamatis Apr 30 '18 at 13:20
  • Can you show us the exact compilation line you use then? Because it works for me. – souki Apr 30 '18 at 13:21
  • you mean this? ||=== Build: Debug in 12345 (compiler: GNU GCC Compiler) ===| obj\Debug\main.o||In function `main':| C:main.cpp|16|undefined reference to `Shape::Shape(std::__cxx11::basic_string, std::allocator >, int)'| C:main.cpp|18|undefined reference to `Shape::getName[abi:cxx11]()'| C:\main.cpp|19|undefined reference to `Shape::getAge()'| C:\main.cpp|16|undefined reference to `Shape::~Shape()'| C:\main.cpp|16|undefined reference to `Shape::~Shape()'| – Nikos Stamatis Apr 30 '18 at 13:23
  • I suppose it is not typed correctly, still have some trouble typing here – Nikos Stamatis Apr 30 '18 at 13:24
  • I mean like the one I told you to try : g++ *.cpp -I./. Are you using Visual studios? Or are you compiling in a terminal? – souki Apr 30 '18 at 13:24
  • Did you put bit.cpp and main in the same folder? Did you change working directory to the folder containing the source code before you type the compilation command? – Pringles Apr 30 '18 at 13:32
  • Yes, I have. Everything is in the same folder, no changes in working directory – Nikos Stamatis Apr 30 '18 at 13:34
  • It means CodeBlocks is only compiling your main. You must change CodeBlocks compiling command to compile Main.cpp and Bit.cpp – souki Apr 30 '18 at 13:35
  • See this question : https://stackoverflow.com/questions/5971206/codeblocks-how-to-compile-multiple-source-files – souki Apr 30 '18 at 13:37
  • I think everything is fine with that, the previous project (a big one) worked, I think I have some errors in code, not compiler but you know better – Nikos Stamatis Apr 30 '18 at 13:38
  • Actually, whitout the typo errors and with the right compilation command, it works fine. So it has to be a compilation command issue. – souki Apr 30 '18 at 13:39
  • `-I./` is redundant – M.M Apr 30 '18 at 13:39
  • Yes, I was wrong, I actually hadn't checked the boxes for the "Bit.h" and "Bit.cpp". Feels bad. Thanks for the help! – Nikos Stamatis Apr 30 '18 at 13:42
  • How do I close it? – Nikos Stamatis Apr 30 '18 at 13:43
  • You just did by selecting my answer as the solution – souki Apr 30 '18 at 13:43
  • Oh, I see ^^. Thanks again :D – Nikos Stamatis Apr 30 '18 at 13:45