Jump to content

"Undefined reference to..." OOP. C++

Basically i just started learning about Object-Oriented Programming and started off with few basic tasks.

I've triple checked everything with an example i had from youtube and all seems to be fine, but i still get an error :
"undefined reference to `SQUARE::SQUARE(int, int, double)"

I have my code in three files:

Header:
 

#include <string>
#include <iostream>

using namespace std;
#ifndef SQUARE_H
#define SQUARE_H

class SQUARE
{
public:

    //Default constructor
    SQUARE(); // nullifies the values
    //Overload constructor
    SQUARE(int,int,double);
    // will asign values to x,y, length
    ~SQUARE();
    // Destructor

    int getNewX() const;
    int getNewY() const;
    double getNewLength() const;
        // get - returns all values

private:
    int newX,newY;
    double newLength;
};
#endif // SQUARE_H_INCLUDED

The one where you define functions:


#include "Square.h"

SQUARE::SQUARE()
{
    newX=0;
    newY=0;
    newLength=0.0;
}

SQUARE::SQUARE(int X, int Y, double Length)
{
    newX=X;
    newY=Y;
    newLength=Length;
}
SQUARE::~SQUARE()
{

}
//**************************************
int SQUARE::getNewX() const
{
    return newX;
}
int SQUARE::getNewY() const
{
    return newY;
}
double SQUARE::getNewLength const
{
    return newLength;
}
//****************************************

And the main:

#include <iostream>
#include <string>
using namespace std;

#include "Square.h"

int main()
{
    int X=0,Y=0;
    double Length=7.0;
    cin>>X;
    SQUARE test(X,Y,Length);// error occurs on declaration

    cout<<test.getNewX(); 
    return 0;
}

Sorry for the long post :P,
maybe someone will find my mistake.
All files are added to the project.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/545875-undefined-reference-to-oop-c/
Share on other sites

Link to post
Share on other sites

Just now, SysVoid said:

I've not really done much C++, but this could be an issue for future:


double SQUARE::getNewLength const
{
    return newLength;
}

Don't you need ()'s there?

I am returning a variable so i don't think so.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

1 minute ago, MisterWhite said:

I am returning a variable so i don't think so.

OK

 

Does C++ have default variables for methods? Maybe you could try that instead of overloading?

 

Also, does anything happen if you change the order of those constructors?

Link to post
Share on other sites

Just now, SysVoid said:

OK

 

Does C++ have default variables for methods? Maybe you could try that instead of overloading?

As my native language is not English and i just started OOP i do not understand what do you mean :). I follow an example from the interweb

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

1 minute ago, MisterWhite said:

As my native language is not English and i just started OOP i do not understand what do you mean :). I follow an example from the interweb

Can you do:

 

SQUARE();
SQUARE(int, int, double);

 

SQUARE::SQUARE(int X = 0.0, int Y = 0.0, double Length = 0.0)
{
    // stuff stuff
}

 

 

Link to post
Share on other sites

Just now, SysVoid said:

 

 


SQUARE::SQUARE(int X = 0.0, int Y = 0.0, double Length = 0.0)
{
    // stuff stuff
}

 

Maybe, i dont know xd. but my problem is not with efficiency  or overloading, just that it "does not see" my class

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

4 minutes ago, fizzlesticks said:

You do need the ().

Do I?

 

https://www. youtube.com/watch?v=b9wialxvcVA

Here i don't.

http://www.learncpp.com/cpp-tutorial/85-constructors/

Here also

 

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

Just now, SysVoid said:

Try putting your #include "Square.h" before using namespace std;

Still the same.

I attached all the files if someone  tries to go through them.

Class - Shapes.cbp

main.cpp

Square.cpp

Square.h

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

1 minute ago, MisterWhite said:

Is "Fraction frac; // Since no arguments, calls Fraction() default constructor " the part you're talking about?

If so, that's only when calling the default constructor, you always need parenthesis when defining a function.

 

What compiler are you using and how are you trying to compile your code?

 

1474412270.2748842

Link to post
Share on other sites

Just now, fizzlesticks said:

Is "Fraction frac; // Since no arguments, calls Fraction() default constructor " the part you're talking about?

If so, that's only when calling the default constructor, you always need parenthesis when defining a function.

 

What compiler are you using and how are you trying to compile your code?

 

Using Code::Blocks, compiler: mingw somthing. I just press F9 or Buil & Run.
Btw are you talking about this:
 

int SQUARE::getNewY() const
{
    return newY; // you mean this should be return newY();?
}

 

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

 

5 minutes ago, MisterWhite said:

Using Code::Blocks, compiler: mingw somthing. I just press F9 or Buil & Run.
Btw are you talking about this:
 


int SQUARE::getNewY() const
{
    return newY; // you mean this should be return newY();?
}

 

34 minutes ago, MisterWhite said:

I am returning a variable so i don't think so.

 

Link to post
Share on other sites

3 minutes ago, MisterWhite said:


Btw are you talking about this:
 


int SQUARE::getNewY() const
{
    return newY; // you mean this should be return newY();?
}

 

No, that's fine since you're just returning a variable.

 

You're missing the () on the line

double SQUARE::getNewLength const

should be

double SQUARE::getNewLength() const

 

1474412270.2748842

Link to post
Share on other sites

Your example works for me.

 

Also this works for me :

struct square
{
public:
    square():_x(0),_y(0),_length(0) {}
    square(int x , int y , int length) : _x(x) , _y(y) , _length(length) {}
    int getX() const
    {
        return _x;
    }
    int getY() const
    {
        return _y;
    }
    double getLength() const
    {
        return _length;
    }
private:
    int _x,_y;
    double _length;
};

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Just now, fizzlesticks said:

No, that's fine since you're just returning a variable.

 

You're missing the () on the line


double SQUARE::getNewLength const

should be


double SQUARE::getNewLength() const

 

Yea, that's my mistake, though the program did not go that far as it encountered with my class being undefined

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

3 minutes ago, MisterWhite said:

Yea, that's my mistake, though the program did not go that far as it encountered with my class being undefined

It works for me. Of course , with that small correction.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

I reckon you haven't linked your class properly. Are you using any IDE? One should have done that for you. Otherwise, you need to do it yourself.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

I tried the project files you uploaded and the problem is that code::blocks isn't compiling the Square.cpp file. I don't know enough about code::blocks to figure out what's wrong but making a new project and copying the files over fixed it for me.

1474412270.2748842

Link to post
Share on other sites

Just now, Nineshadow said:

I reckon you haven't linked your class properly. Are you using any IDE? One should have done that for you. Otherwise, you need to do it yourself.

Yea Code::blocks. I have added all my files. i believe everything is linked.
keep in mind just starting to swim in this ocean

Class - Shapes.cbp

main.cpp

Square.cpp

Square.h

 

i will check LTT tomorrow.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

Just now, MisterWhite said:

 

Using g++ , you basically need to compile both cpps into object files.

g++ -c main.cpp
g++ -c SQUARE.cpp

Then compile your program from the resulting object files :

g++ -o square.exe main.o SQUARE.o

Of course, assuming that all the files are in the same directory.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

1 minute ago, MisterWhite said:

Yea Code::blocks. I have added all my files. i believe everything is linked.
keep in mind just starting to swim in this ocean

Class - Shapes.cbp

main.cpp

Square.cpp

Square.h

Ah  , if you are using Code::blocks just make sure you also compile Square.cpp , not just main.cpp .

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

16 hours ago, Nineshadow said:

Ah  , if you are using Code::blocks just make sure you also compile Square.cpp , not just main.cpp .

Thing is, this is not my first time using several files when working with OOP and it is the first time i get this error.

EDIT: oh, wow, removed all files from the project, added them and everything works... Thanks for the help though

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×