Very beginner need help with object member

I have no idea why my code is not working.
i know its probably very simple but I started learning c++ a week ago and cant figure out why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//main.cpp-------------

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

using namespace std;

int main(){

    Sally::so;
    cout << "hello world" << endl;

//Sally.h--------------

#ifndef SALLY_H
#define SALLY_H


class Sally
{
    public:
        Sally();
        ~Sally();

    protected:
    private:
};

#endif // SALLY_H

//Sally.cpp------------

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

Sally::Sally()
{
    cout << "constructor" << endl;
}

Sally::~Sally(){
cout << "destructor" << endl:
}


Also if you know of the best free c++ and java video tutorials please let me know about them.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

class Sally
{
   public:
   Sally();
   ~Sally();
};

Sally::~Sally()
{
   cout << "destructor" << endl;
}

Sally::Sally()
{
    cout << "constructor" << endl;
}

int main()
{
   Sally so;
   cout << "hello world" << endl;
}


Where you combine the files into one file the class definition must come before main().
Last edited on
I am actually using 3 files, I'm just now learning how to use separate header files and cant get it to work. Here are each of my files broken into different code:
This is my main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Sally.h"

using namespace std;

int main(){

    Sally so;
    cout << "hello world" << endl;

}


Here is my Sally.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef SALLY_H
#define SALLY_H


class Sally
{
    public:
        Sally();
        ~Sally();

    protected:
    private:
};

#endif // SALLY_H 


and here is my Sally.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Sally.h"
#include <iostream>
using namespace std;

Sally::Sally()
{
    cout << "constructor" << endl;
}

Sally::~Sally(){
cout << "destructor" << endl:
}


The error I am getting in codeblocks is:
||=== Build: Debug in learning2 (compiler: GNU GCC Compiler) ===|
C:\ccnDP4et.o||In function `main':|
C:\main.cpp|8|undefined reference to `Sally::Sally()'|
C:\main.cpp|9|undefined reference to `Sally::~Sally()'|
C:\main.cpp|9|undefined reference to `Sally::~Sally()'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|



so frustrated and I just got started!
closed account (48T7M4Gy)
OK just wait I'll have a look.
You are many steps ahead of me. I do not even know what to do after writing the Hello World program. I tried to compile it thus:

clipper hello.prg as I would a in "Clipper language".

Got error message. Am in a cul desac now.
Could I have generated the class with the wrong settings?
thank you for your help :)
closed account (48T7M4Gy)
line 11 in sally.cpp has got a ':' at the end instead of a ';'

Provided all three files are recognized as being in codeblocks path for that project then your program is working correctly.
Last edited on
Ok I fixed that and my error is:

||=== Build: Debug in learning3 (compiler: GNU GCC Compiler) ===|
C:\Temp\ccwEECPH.o||In function `main':|
C:\main.cpp|8|undefined reference to `Sally::Sally()'|
C:\main.cpp|9|undefined reference to `Sally::~Sally()'|
C:\main.cpp|9|undefined reference to `Sally::~Sally()'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Sally.h"

using namespace std;

int main(){

    Sally so;
    cout << "hello world" << endl;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef SALLY_H
#define SALLY_H


class Sally
{
    public:
        Sally();
        ~Sally();

    protected:
    private:
};

#endif // SALLY_H 

1
2
3
4
5
6
7
8
9
10
11
12
#include "Sally.h"
#include <iostream>
using namespace std;

Sally::Sally()
{
    cout << "constructor" << endl;
}

Sally::~Sally(){
cout << "destructor" << endl;
}
closed account (48T7M4Gy)
I'll give you the three files I have run successfully on my machine.

1
2
3
4
5
6
7
8
9
10
11
12
13
// sally.cpp
#include "Sally.h"
#include <iostream>
using namespace std;

Sally::Sally()
{
	cout << "constructor" << endl;
}

Sally::~Sally() {
	cout << "destructor" << endl;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//sally.h
#ifndef SALLY_H
#define SALLY_H


class Sally
{
public:
	Sally();
	~Sally();

protected:
private:
};

#endif // SALLY_H 


1
2
3
4
5
6
7
8
9
10
11
12
// source.cpp
#include <iostream>
#include "Sally.h"

using namespace std;

int main() {

	Sally so;
	cout << "hello world" << endl;

}


constructor
hello world
destructor


Just cut and paste if absolutely necessary but what you should do is examine it closely.

Last edited on
I copy and pasted your code into 3 files called "main.cpp", "Sally.h" and "Sally.cpp" and I am getting the same error message...
I think something is wrong with my codeblocks...
closed account (48T7M4Gy)
Maybe it's codeblocks but I reckon it is the directory structure and there is a disjoint between the header and the cpp files.

Even if you use VStudio which is slightly better, unless you add the files to the project properly they will be like ships passing in the night, hence the errors you're getting. It's a while since I used codeblocks but from memory you just select the files and drag/drop them into the project.
Ok well I created a new project and added a class via File>New>Class...
I looked over the class settings and checked the files are all in the same directory and unchecked virtual destructor. I then pasted all of your code and build and still got the same error.

I dont know why the header and cpp files are not reading each other it is really stopping my learning!
closed account (48T7M4Gy)
I don't have an answer for you I'm afraid.

The code runs properly and if it is in the right locations and you have no typos in the file names and corresponding #includes then ...

One trick also with the new project is to create a new hello world project and cut/paste/edit main into that, and then create a new class. That way the system automatically (should) handle the locations.

You can test if the header is being read by just combining the .h and .cpp files together as one .h file which is sort of where we started off.

Keep me posted if you like.
I tried the trick you suggested and it did not fix it. I then combined the files into one and it worked, I then separated them and got the error again: "Undefined reference to header::"

I uninstalled and reinstalled code blocks before I did any of this...

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

I may need to try a new free compiler, any suggestions?
closed account (48T7M4Gy)
VStudio 2015 for a PC and if you have a Mac the Xcode is the best
Thanks for all your help!! I tried the same code today and it worked. I just opened the saved project and ran it with o problems. Still confused though
closed account (48T7M4Gy)
Glad it worked - maybe it needed a 'Clean' and 'Rebuild' (see menus if you're not sure of the meaning)

Cheers
Topic archived. No new replies allowed.