Getting the error "Myclass undeclared identifier" in Visual Studio when using separate class file.

I've created a separate class file and wrote a very simple cout function to print something to the screen, this was only to see if i could make it work.

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

This was the video i was trying to follow and have a few differences from the code used in the video which i assume is because im using Visual Studio instead of Codeblocks.

One difference is, when i created the class, i was presented with two prototypes in the header file instead of just the one i expected.

This is what my header file looks like :

1
2
3
4
5
6
7
1 #pragma once
2 class Myclass
3 {
4 public:
5 	Myclass();
6 	~Myclass();
7 };


I don't know what the difference is between the two or what to do with the second one, if someone could clarify that would be great.

Also ill provide all my code because i don't know what people will need and what they won't, thanks for the help.

My main .cpp

1
2
3
4
5
6
7
8
9
10
  #include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	Myclass Object;
	return 0;
}


My header file.

1
2
3
4
5
6
7
8
9
#pragma once
class Myclass
{
public:
	Myclass();
	~Myclass();
};



And lastly my class .cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include "Myclass.h"
#include <iostream>

using namespace std;


Myclass::Myclass()
{
	cout << "Test for seperate class file" << endl;
}


Myclass::~Myclass()
{
}


If any more information is needed, i will reply. Thanks again.

Edit: My main question is, how do I get my program to actually work, and what is the cause of the error "Myclass undeclared identifier?"

Last edited on
closed account (48T7M4Gy)
Myclass::~Myclass()

This one is called the destructor (as opposed to constructor). It is used when an object goes out of scope. Generally you don't have to do anything with it unless there is special memory cleanup etc. say if a class member/properties uses 'new' in it's instantiation.

https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html#zz-2.19
Last edited on
Thanks, at least i know what it is now in case i run into problems further down the road.
Any idea how to get my code to actually work though? i cannot for the life of me figure out what is wrong with it.
closed account (48T7M4Gy)
If you want to see how the destructor works put cout << "destructor" << endl; between the braces in the .cpp file.

You need to #include "Myclass.h" in main.cpp
Topic archived. No new replies allowed.