P
P
Perzh2014-07-02 07:43:43
C++ / C#
Perzh, 2014-07-02 07:43:43

Why is the class definition not visible?

Hello. I am new to C++. Programming in VisualStudio2008 There was a problem when including header files. I have file MyClasses.h:

#ifndef MYCLASSES_H
#define MYCLASSES_H
namespace my {
    // Some template classes
    ...
    MyClass {declaration} // My class (not template) contains static methods only
   // Other classes declaration
};
#endif

Accordingly, there is a MyClasses.cpp file:
namespace my {
    // implementation of MyClass and other classes
}

MyClass consists of several static methods and that's it (no variables). The header file has already been used at least once in another project (but in the same solution). Now there is a need to connect it to the same project in which it (MyClasses.h) is defined. But when connecting, I can't use either MyClass::method() or my::MyClass::method() (error C2653: 'MyClass' : is not a class or namespace name + error C3861: 'method': identifier not found ). I tried to declare (just declare) MyClass in the file where I want to include it:
#include "MyClasses.h"
class MyClass;

but the compiler does not see the class implementation (error C2027: use of undefined type 'MyClass').
Please tell me the unlucky one, how to connect MyClasses.h correctly? Thanks in advance
UPD: If I just do #include "MyClasses.h"I get
error C2653: 'MyClass' : is not a class or namespace name
on the line
MyClass::StaticMethod(); // или так my::MyClass::StaticMethod();

If I use class forward declaration like this
#include "MyClasses.h";
class MyClass;
or so
#include "MyClasses.h";
namespace my { class MyClass; }
then I get an error error C2027: use of undefined type 'my::MyClass'on the linemy::MyClass::StaticMethod();

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sumor, 2014-07-02
@Perzh

See if you are using Precompiled headers.
If so, is there a first line entry in each .cpp
Good article on Habré

M
Misha Krinkin, 2014-07-02
@kmu1990

What is the first error the compiler reports? And it would be nice to show how you define the MyClass class, there is a suspicion that you just have a mistake there.
Also, this is not particularly relevant, but it looks like you have a mistake here:

#ifndef MYSECLASSES_H
#define MYCLASSES_H

such guards do not protect against re-enablement - it is worth fixing it.
UPD: you show the class definition MyClass:
(although your abbreviation is kind of weird), but use the MyClasses class:
you already decide what you need, MyClass or MyClasses?

E
Eugene, 2014-07-02
@EvgenijDv

As already written above, show your class declaration file and preferably a complete list of compilation errors associated with this class.

X
xandox, 2014-07-02
@xandox

strange design, what did you want to tell her?

#include "MyClasses.h"
class MyClass;

if you need a forward declaration, then do so
if you need a variable, then
to UPD
Listen, you have some kind of complete mess
. First of all, decide what your class is called MyClass or MyClasses. By the fact that you have one thing written in your code, and another is written in the error.
Secondly, why do you need a semicolon after the inclusion?
And yes, show me your header and the place where you are trying to use it. And yet - do you additionally have an error that it was not possible to find MyClasses.h ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question