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)

13
  • 2
    How do you compile this? Commented Apr 30, 2018 at 12:58
  • Sorry that I didn't add this above. GNU GCC Compiler and build target is main.cpp Commented Apr 30, 2018 at 13:02
  • 1
    You need to compile and link every cpp file, not just main.cpp Commented Apr 30, 2018 at 13:08
  • There are tons of typos or mistakes in the files. (Looks like some have since been fixed.) Once I fixed those, it compiled and ran correctly for me. Commented Apr 30, 2018 at 13:08
  • 2
    Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it? Commented Apr 30, 2018 at 13:10

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

16 Comments

That was a typo, indeed, still the same errors appear
Can you show us the exact compilation line you use then? Because it works for me.
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<char, std::char_traits<char>, std::allocator<char> >, 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()'|
I suppose it is not typed correctly, still have some trouble typing here
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.