0

Hi,

I'm using Codeblocks as a compiler. So when i'm creating a new class by following File>New>Class it creates two files xxxx.h xxxx.cpp

Ok, code is simple.

#include <iostream>
#include "Sally.h"
using namespace std;

int main(){

Sally so;
}

Sally.h

#ifndef SALLY_H
#define SALLY_H


class Sally
{
    public:
        Sally();
    protected:
    private:
};

#endif // SALLY_H

Sally.cpp

#include "Sally.h"
#include <iostream>
using namespace std;

Sally::Sally()
{
  cout << "i'm a constructor" << endl;
}

When i'm trying to run this it give an error

C:\Users\Artjom\Desktop\My Project\main.o:main.cpp|| undefined reference to `Sally::Sally()'|
||=== Build finished: 1 errors, 0 warnings ===|

Please can you tell me what is wrong?

Thanx

Edited by Raisefamous: n/a

4
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by Raisefamous
0

Hey there (Think this is what you're looking for):

First off your class:

class Sally
{
    public:
        Sally(); 

    private:
     
};

Only have public and private/protected etc.. Not twice =)

And the cpp file:

#include<iostream>
#include<string>
#include "Sally.h";

Sally::Sally(){};

Main:

#include <iostream>
#include "Sally.h"
using namespace std;

int main(){
    
    Sally so;
    
    return 0;
}

Should compile now =)

0

Hi,

I don't think any problem with code. You can check the make files if there any (ie., created by your tool itself).

Thanks,
KMat
------------------------------------------------------------------------------------

Hi,

I'm using Codeblocks as a compiler. So when i'm creating a new class by following File>New>Class it creates two files xxxx.h xxxx.cpp

Ok, code is simple.

#include <iostream>
#include "Sally.h"
using namespace std;

int main(){

Sally so;
}

Sally.h

#ifndef SALLY_H
#define SALLY_H


class Sally
{
    public:
        Sally();
    protected:
    private:
};

#endif // SALLY_H

Sally.cpp

#include "Sally.h"
#include <iostream>
using namespace std;

Sally::Sally()
{
  cout << "i'm a constructor" << endl;
}

When i'm trying to run this it give an error

C:\Users\Artjom\Desktop\My Project\main.o:main.cpp|| undefined reference to `Sally::Sally()'|
||=== Build finished: 1 errors, 0 warnings ===|

Please can you tell me what is wrong?

Thanx

0

Yes, I think so.. you need to check your compiler.
Which compiler are you using?

I have tried the same code and used the below command to compile and it worked well for me.
I am using Linux(Ubuntu).

g++ -o Sally main.cc Sally.cc -I.

/KMat

It still give the same error, i think something wrong with the compiler.

0

Yes, the problem was that sally.cpp and sally.h were stored in the different folder. Don't know how that happened.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.