Rogue
Rogue

Reputation: 676

My class isn't defined in the scope?

I'm just starting to learn C++ as an already-experienced programmer in several other languages. The issue I'm having is probably a very obvious error on my end.

I have a class called Test in its own file, and it also has the respective header file. However, when I try to create an instance of it in main, I get this error:

Error: Test was not declared in this scope.  
Error: Expected ';' before 'to'

This is my main:

#include <iostream>

using namespace std;

int main()
{
    Test to;
    return 0;
}

This is the Header for Test:

#ifndef TEST_H
#define TEST_H


class Test
{
    public:
        Test();
};

#endif // TEST_H

This is the Test Class:

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

using namespace std;

Test::Test()
{

}

As you can see it is a simple class with an empty constructor. I have no idea what I could possibly be doing wrong here, so any help is much appreciated.

Edit: Thanks for the help, I knew it would be something stupidly obvious :P

Upvotes: 1

Views: 71

Answers (2)

Tyler Jandreau
Tyler Jandreau

Reputation: 4335

You forgot to include the header containing the Test class so the main can "see" it. Try this code instead:

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

int main()
{
    Test to;
    return 0;
}

Upvotes: 1

Ben Zotto
Ben Zotto

Reputation: 70998

#include "Test.h"

in your main file also.

Upvotes: 5

Related Questions