I'm working on a project in c++ and I stuck with no idea what is wrong. I've writen 4 classes and everything looked fine during work (under visual studio 2010). VS 'saw' all the definition, i could use auto-fill and sudgestions, but when I tried to compile the project it sudennly went blind. It's like I didnt include headers or something (which I did). The strange thing is there is no problem with working with those classes on VS (i can ctrl+space for hint, list of attributes and methods and all that stuff) but when i try to compile it says "ClassName" is not a type.
Quick sample of problem below:
ProButton.cpp:
#include "ProButton.h"
using namespace pGUI;
ProButton::ProButton( ... )
: ProControl( ... )
{
...
}
ProButton.h:
#ifndef __PRO_BUTTON__
#define __PRO_BUTTON__
#include <string>
#include "ProControl.h"
namespace pGUI
{
class ProButton :
public pGUI::ProControl
{
public:
//attributes
...
public:
//methods
...
};
}
#endif
but compiler says:
Error 291 error C2653: 'ProButton' : is not a class or namespace name
for this line in ProButton.cpp: ProButton::ProButton( ... )
It also says:
Error 23 error C2039: 'ProControl' : is not a member of 'pGUI'
Error 24 error C2504: 'ProControl' : base class undefined
and all similar errors for whole project. I have no idea what is wrong. Looks like my VS broke :D
Of course those (...) means there is code there, just not that important for now. I can upload all solution somewhere fi it will help.
edit//
About namespaces, all header files (classes declaration) are defined in namespace with:
namespace pGUI{
class ProClass
{
};
}
all definitions for these classes (in ProClass.cpp) are using:
using namespace pGUI;
at the beginning.
I think the problem is with order of including files.
Im not sure how this is supposed to be done. So far i have 4 classes that:
class ProGUI:
has a pointer to ProContainer
includes: ProContainer and ProControl
class ProContainer:
has pointers to: ProGUI and ProControl
class ProControl:
has a pointer to ProContainer
includes ProButton
is a base class for ProButton
class ProButton:
is a sub-class of ProControl
Those classes also uses irrlicht library and I'm not sure where to include it.
I had it included in my main file just before #include "ProGUI.h". This is also the only include in main. ProGUI.h .
//EDIT 2 -> solved
It was a problem with includes. I needed to rethink my inclusion order and add some forward declarations. Anyway that all seemed strange and took me a lot of time to figure i out. Thx for help. :)
It seems that you are using following statement:
using namespace pGUI;
Just before the class declaration:
class ProControl
{
};
Instead of using following approach:
namespace pGUI
{
class ProControl
{
};
}
The using namespace, as it says uses a namespace. You need to explicitly put something a namespace using namespace keyword followed by braces!
using namespace pGUI informs the compiler that it should look in the pGUI namespace to resolve existing names.
To declare or implement something in a namespace you need to be more specific. with either:
namespace pGUI
{
ProButton::ProButton( ... ) : ProControl( ... )
{
...
}
}
or:
pGUI::ProButton::ProButton( ... ) : ProControl( ... )
{
....
}
Personally, I consider any use of using namespace to be a lazy programmer hack that completely defeats the point of namespaces. But I digress. :)
Related
I have three classes.
first class:
#ifndef C_LINKED_LIST_H
#define C_LINKED_LIST_H
class CLinkedList {
private:
//removed code for brevity
public:
// removed code for brevity
};
#endif
second class:
#ifndef C_SSF_FOLDER_CONTAINER_H
#define C_SSF_FOLDER_CONTAINER_H
#include "C_SSF_Folder.h"
#include "CLinkedList.h"
class C_SSF_Folder_Container {
private:
// removed code for brevity
public:
int Add_Folder(C_SSF_Folder *_pcl_SSF_Folder);
C_SSF_Folder *Get_Folder(int _i_Index);
C_SSF_Folder *Get_Folder(char *_pch_Name);
//^-----errors
};
#endif C_SSF_FOLDER_CONTAINER_H
my third class
#ifndef C_SSF_FOLDER_H
#define C_SSF_FOLDER_H
#include <windows.h>
#include <fstream>
#include "C_SSF_Folder_Container.h"
using namespace std;
class C_SSF_Folder {
public:
private:
C_SSF_Folder_Container cl_SSFFC_Folder_Container;
public:
};
#endif
my third class C_SSF_Folder.
I am including "C_SSF_Folder_Container.h"
and declaring a C_SSF_Folder_Container container.
Before declaring the variable it compiles fine. After I declare it
I get syntax errors in my C_SSF_Folder_Container
Severity Code Description Project File Line Suppression State
Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem\projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 16
Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem \projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 19
As I myself look into it I think there is a problem because my C_SSF_Folder is including C_SSF_Folder_Container.
and C_SSF_Folder_Container is including C_SSF_Folder
but the defines should take care of it? Other than that I have no clue what's the problem.
Everything is typed correctly.
You've got a circular #include -- C_SSF_Folder_Container.h #includes C_SSF_Folder.h and C_SSF_Folder.h #includes C_SSF_Folder_Container.h.
This would cause an infinite regress (and a compiler crash) except that you've got the #ifndef/#define guards at the top of your files (as you should); and because of them, instead what you get is that one of those two .h files can't see the other one, and that's why you get those errors.
The only way to fix the problem is to break the circle by deleting one of the two #includes that comprise it. I suggest deleting the #include "C_SSF_Folder.h" from C_SSF_Folder_Container.h and using a forward declaration (e.g. class C_SSF_Folder; instead.
C_SSF_Folder.h and C_SSD_Folder_Container.h are including each other(Circular Dependency).
When the compiler compiles C_SSF_Folder_Container object, it needs to create a C_SSF_Folder object as its field, however, the compiler needs to know the size of C_SSF_Folder object, so it reaches C_SSF_Folder object and tries to construct it. Here is the problem, when the compiler is constructing C_SSF_Folder object, the object has a C_SSF_Folder_Container object as its field, which is a typical chicken and egg question, both files depends on each other in order to compile.
So the correct way to do it is to use a forward declaration to break the circular dependency(including each other).
In your C_SSF_Folder.h, make a forward declaration of C_SSF_Folder_Container.
#include <windows.h>
#include <fstream>
using namespace std;
class C_SSF_Folder_Container;
class C_SSF_Folder {
public:
private:
C_SSF_Folder_Container cl_SSFFC_Folder_Container;
public:
};
#endif
Finally, include C_SSF_Folder_Container.h in your C_SSF_Folder.cpp.
You can also learn more in the following links:
Circular Dependency (Wiki):
https://en.wikipedia.org/wiki/Circular_dependency
Forward Declaration by Scott Langham
What are forward declarations in C++?
I am trying to make a managed .dll in c++ that requires the support for multithreading. I am developing in visual Studio 2013, using platform toolset version v120. the reason I need this to be a managed assembly is because it is required to integrate the assembly in LabView.
following the steps in Creating and Using a Managed Assembly in VC++ 2010 gives good results. but I obviously need to implement something more complicated and when I include threading and write the following code:
#pragma once
#include <thread>
using namespace System;
using namespace std;
namespace MultiThread_module {
public ref class multiThreadingTest
{
public:
String^ GetVersion();
int someNumber;
private:
thread testThread;
};
}
I get following errors:
"thread" is not supported when compiling with /clr or /clr:pure.
a member of a managed class cannot be of a non-managed class type
error directive: ERROR: Concurrency Runtime is not supported when
compiling /clr.
error directive: is not supported when compiling with /clr or
/clr:pure.
A friend of mine says it is impossible to write multi-threaded code in Visual Studio without using external packages like boost. It kind of seemed unlikely since Multithreading has already been already there for C# and VB for a long time!
So, I would be happy if you could let me know what I am doing wrong OR if it is really hard to have a managed multithreaded .dll developed in c++?
You can use the managed thread library: System.Threading.Thread.
#pragma once
using namespace System;
using namespace std;
using namespace System::Threading;
namespace MultiThread_module {
public ref class multiThreadingTest
{
public:
String^ GetVersion();
int someNumber;
private:
Thread^ testThread;
};
}
If it's purely CLR then I suggest you use the example provided before. If you want to have the threading completely native and just use CLR to wrap it, I'd like to refer you to my answer at : using clr and std::thread
Might be an old question, but I looked into this same problem before. Since CLR does not allow you to include std::thead at
compile time, you could try to use it only at linking time. Normally
you could resolve this be forward declaring the class in your header
and including them only in your cpp files. However you can forward
declare your own classes in header files, but you can't for
classes in namespace std. According to the C++11 standard, 17.6.4.2.1:
The behavior of a C++ program is undefined if it adds declarations or
definitions to namespace std or to a namespace within namespace std
unless otherwise specified.
A workaround for this problem is to create a threading class that
inherits from std::thread that you can forward declare. The header
file for this class would look like:
#pragma once
#include <thread>
#include <utility>
namespace Threading
{
class Thread : std::thread
{
public:
template<class _Fn, class... _Args> Thread(_Fn fn, _Args... args) : std::thread(fn, std::forward<_Args...>(args...))
{
}
private:
};
}
In the header file that you would like to use the thread you can do
forward declare it like:
#pragma once
// Forward declare the thread class
namespace Threading { class Thread; }
class ExampleClass
{
public:
ExampleClass();
void ThreadMethod();
private:
Threading::Thread * _thread;
};
In your source file you can then use the theading class like:
#include "ExampleClass.h"
#include "Thread.h"
ExampleClass::ExampleClass() :
{
_thread = new Threading::Thread(&ExampleClass::ThreadMethod, this);
}
void ExampleClass::ThreadMethod()
{
}
Hope it might help anyone.
I'm trying to integrate c++ code with awesomium functionalities, but I get many errors.
It seems that VisualStudio doesn't like my definition/declaration of the WebCore element. I copied it from http://wiki.awesomium.com/tutorials/tutorial-1-hello-awesomium.html.
I have simplified the code until this, and I still get the errors.
SimpleClass.cpp:
#include <Awesomium/WebCore.h>
include "SimpleClass.h"
using namespace Awesomium;
CSimpleClass::CSimpleClass(){
WebCore *web_core = WebCore::Initialize(WebConfig());
}
CSimpleClass::~CSimpleClass(){
}
SimpleClass.h:
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
WebCore *web_core;
};
Thanks!
Change your SimpleClass.h header to read:
#pragma once
#ifndef SIMPLECLASS_H
#define SIMPLECLASS_H
// forward declarations
namespace Awesomium{
class WebCore;
}
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
Awesomium::WebCore *web_core;
};
#endif /* SIMPLECLASS_H */
That way you announce to your compiler that there exists a type WebCore in the namespace Awesonium, and then you can use it to declare the member pointer CSimpleClass::web_core.
Potential dependency issues aside, the problem is that your header does not know that you want to use the Awesomium namespace.
Either (preferred) be explicit in the header about your definition of *web_core by doing
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
Awesomium::WebCore *web_core; //note the use of Awesomium::
};
or (if you really must) include your header after your using directive
#include <Awesomium/WebCore.h>
using namespace Awesomium;
#include "SimpleClass.h"
Goal: To inherit from a class with 2 template parameters.
Error
Error error C2143: syntax error : missing ',' before '<'
The Code
template< typename Type >
class AssetManager : public IsDerivedFromBase< Type, Asset* >
{
// Class specific code here
};
Things you need to know: Asset is a class that just wraps a char* with a getter/setter and IsDerivedFromBase will be used to basically test wether or not Type is a derivative of Asset. These sets of classes are isolated in there own small scale visual Studio 2012 project and will be integrated into the main project once all the functionality has been tested thoroughly.
Some Edits based on Comments
Thank you for the help so far, I really appreciate it. Here are some more specifics:
IsDerivedFromBase.h
#ifndef ISDERIVEDFROMBASE_H
#define ISDERIVEDFROMBASE_H
#include< iostream > // For access to NULL
namespace Fenrir
{
template< typename Type, typename Base >
class IsDerivedFromBase
{
private:
static void Constraints( Type* _inheritedType )
{
Base* temp = NULL;
// Should throw compiler error if
// this assignment is not possible.
temp = _inheritedType;
}
protected:
IsDerivedFromBase( ) { void( *test )( Type* ) = Constraints; }
};
}
#endif
Note: This class was based off of a class I read about in an article. I have since found a better way to achieve the desired result; however, I wish to understand where this error is stemming from.
AssetManager.h"
#ifndef ASSETMANAGER_H
#define ASSETMANAGER_H
#include "IsDerivedFromBase.h"
namespace Fenrir
{
template< typename Type >
class AssetManager : public IsDerivedFromBase< Type, Asset* >
{
// Class specific code
};
}
#endif
Keeping class specific code to a minimum to keep this post as neat as possible, if more info is needed just let me know and I can add it in :).
That error message is common when the compiler encounters an identifier that it did not expect, so a first guess would be that IsDerivedFromBase is not known to the compiler at the time (maybe you did not include the appropriate header?). Alternatively, if IsDerivedFromBase is not a template, the compiler would also expect a , (or ;) after it.
Solved my issue which was interestingly silly. So due to a comment (from Jesse Good) I took a quick look at my includes. Since this was a small "whip it up quick" kind of project I didn't really pay much attention to them. I don't know exactly where the error was occuring, but I found out that AssetManager did not know about IsDerivedFromBase so I set up the following code block to just solve that issue without having to write a bunch of
#include statements everywhere!
// Contact point houses all of the include files
// for this project to keep everything in one place.
#ifndef CONTACTPOINT_H
#define CONTACTPOINT_H
#include "Asset.h"
#include "Sprite.h"
#include "IsDerivedFromBase.h"
#include "GenericManager.h"
#include "AssetManager.h"
#endif
Now I just include this in every header and all is well. I've been programming C++ for a little over a year now and never ran into this, a good lesson to learn for anyone new programming.
Thanks for your help everyone : )
This is a very frustrating error message in Qt Creator: ’XYZ’ does not name a type. This usually means that there is an error in the class XYZ that prevents the compiler from generating the type, but there are no additional hints as to what went wrong.
Any suggestions?
I found this problem on qtcreator 3.4.1 and QT 5.4, when I replace such as
#include <QTextEdit>
with
class QTextEdit;
this problem gone.
i just had this problem, and like Arckaroph have said :
the problem is that when we include a header file in a source code file, and we use in it the directive #ifndef, we can't include it again in a header file to give a type of it's included class to a variable in source code file
example :
class1.h contains Class1
class2.h contains Class2
class2 have a private variable V with class1 type
if we include class1.h in class2.CPP we can't include it in class2.h to give V a class1 type.
so we put in class2.cpp class2.h before class1.h
or we delete class1.h from class2.cpp
In your abc.cpp make sure you include xyz.h before including abc.h.
No idea why swapping the two around would make a difference however it did for me.
Do you get the error from the compiler or the IDE (as a squiggly underline)? I've encountered this in Qt Creator 1.2.9 and I think it's a bug in the IDE.
I believe you are declaring something of type XYZ such as
XYZ foo;
The problem is XYZ is not yet defined.
The following is my problem and my conclusion. What do you think?
My problem is I have a class ABC and a class XYZ. Class ABC has a member that is declared as a XYZ type. Class XYZ has a member that is declared as a ABC type. The compiler doesn't know what the XYZ type is yet because it has not defined it yet. Therefore the error given is 'XYZ' does not name a type.
Example Code:
class ABC{
private:
XYZ *xyz; //XYZ is not defined yet
};
class XYZ{
private:
ABC *abc; //ABC is defined above
};
I found a solution for myself.
Say I have class A and class B.
"A.h" includes "B.h" and has instance of B as member.
"B.h" includes "A.h" and has instance of A as member.
Compiler gives me an error in "B.h" on line of code where member of class A is declared:
"A doesn't name type"
What I do is in "A.h" I remove #include "B.h" and place #include "B.h" to "A.cpp". And before A class declaration I write class B;
...
// #include "B.h"
class B;
class A
{
...
B m_b;
};
...
Worked for me, Good luck!
in a recent QT project, where I had just installed the most recent QT (3/2011), I cured the three of these that was stopping my build by adding this...
#include <sys/types.h>
...prior to the include of the header file that was throwing the errors. That did it.
I don't know why they would distribute something that had such problems, perhaps in other systems types.h is included with something else, but not in my case, anyway. Hope that helps someone.
Does #include'ing the corresponding header file help?
If you're using templates, then you need to precede the class name with "typename" so that the compiler can recognize it as a type...
template <typename t> //...
Two possibilities occur to me:
1. Perhaps you have SLOT instead of SIGNAL in a connect() call.
2. Sometimes it helps to make a gratuitous edit to the .PRO file (e.g. insert and delete a space), so that QMake gets run and the .moc files get generated.
In my case I wasn't using the namespace the class was defined in. Contents of the header were contained within the namespace, but the source file was missing the using namespace directive.
.h:
namespace mynamespace {
class MyClass : public QWidget
{
...
}
}
.cpp:
using namespace mynamespace
MyClass::MyClass(QWidget *parent) : QWidget(parent)
{
}