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

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 =)

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

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

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.

You need to add Sally.cpp to the project.

Menu: Project->Add Files ... IIRC.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.