I have a struct that is declared in a C++ header file with the line:
struct AII_Common_Export message{
...
};
I am then trying to create an instance of that struct in a C++ source file, so that I can set/ use some of the attributes stored in the struct:
message data;
However, when I compile my code, I get an "undeclared identifier" error on this line... I have included the header file in the source file, so I don't understand why I am getting this error- can someone explain it to me?
I also tried creating the instance of it with:
AII_Common_Export message data;
But then got the compile error: "syntax error: missing ';' before identifier 'data'.
Any ideas how I can fix this, so that I can create an instance of the struct?
EDIT
I have just found the AII_Common_Export definition- it is defined with:
# define AII_Common_Export ACE_Proper_Import_Flag
and the ACE_Proper_Import_Flag is defined with:
#define ACE_Proper_Import_Flag __declspec (dllimport)
These two definitions are in separate header files.
Just do.
struct message{
...
};
message data;
see http://www.cplusplus.com/doc/tutorial/structures/
Related
this is linked to Redefinition and Enumerator and Reusing enum values in separate enum types and similar issues.
I have a VS2013 project using a header and lib from an external source which I cannot get a new version from.
When I upgrade to v142/VS2019 and compile I get the error "C2365 'PS_DEFAULT': redefinition; previous definition was 'enumerator'" from the external header.
I cannot find the enum PARA_STATE or PS_DEFAULT anywhere else in the code except in the header.
The header code looks like this:
#ifndef GLX_DBMS_H
#define GLX_DBMS_H
...
enum PARA_STATE
{
PS_NDEF,
PS_DEFAULT,
PS_DEFAULT_LONG,
PS_PARAM,
PS_PARAM_LONG,
PS_CONNECTED
};
struct Para
{
DATA_TYPE paraType;
WORD paraTypeLength;
char paraName[SIZEPARANAME];
PARA_STATE strucType;
}
struct ParaV8
{
DATA_TYPE paraType;
WORD paraTypeLength;
char paraName[SIZEPARANAME];
PARA_STATE strucType;
}
...
#endif
What changed between 2013 and 2019 so this no longer works?
Is there a way to fix this outside of getting a new version from the external?
UPDATE
I looked at Messages in Visual studio as suggested and line 3835 in ShObjIdl_core.h has an enum with PS_DEFAULT so that is the cause.
I tried two different "solutions" which both work, but are hacks.
I added class to the enum in the windows header line 3828 and it compiled. This is only a local solution though
enum class PLACEHOLDER_STATES
Change the name in my external header file, I think I will only get away with this since the enum is not used in the rest of the code.
enum PARA_STATE
{
PS_NDEF,
PS__DEFAULT
};
If I see correctly ShObjIdl_core.h is for COM and the external library uses COM so I am not able to get rid of the dependency.
Any better solutions?
In my case
I created typedef enum { ME1 = 0,ME2 }MyEnum; in MDefine.h header file.
And I was trying to use ME1 in CtrlBit.h
And the main problem was
NOT DOING #pragma once in MDefine.h which has MyEnum.
(yeah!! I was sure that it's not possible that my variables declared in other files..)
Also, I include MDefine.h in stdafx.h
And yeah! My problem was solved))
I am working on some simulation software, written in C++. Currently, when trying to build my code, I get 9 compile errors, all complaining about the three lines of code that I've just added. Those lines are the declarations in PublisherModule.h:
class PublisherModule :
public s::Module,
public s::Singleton<PublisherModule>,
public s::Interface,
public s::htmlPage{
public:
...
Types::ModeRecord;
ModeRecord modeData;
ModeRecord *modeDataPtr;
...
};
The reason for adding these declarations into the PublisherModule.h was so that I could add the following code into the publish() function of the PublisherModule.cpp file:
Types::ModeRecord ModeData;
Types::ModeStatusRecord *ModeDataPtr = &ModeData;
DataStore->getModeData(*ModeDataPtr);
The publish() function now looks like this:
void PublisherModule::publishData(void){
...
Types::ModeRecord ModeData;
Types::ModeStatusRecord *ModeDataPtr = &ModeData;
DataStore->getModeData(*ModeDataPtr);
...
}
The errors that the compiler is giving me are:
error C2653: 'Types': is not a class or namespace name
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error : missing ';' before identifier 'ModeData'
error C2143: syntax error : missing ';' before '*'
I get error C4430 4 times in total, and the other errors all once each. They are all complaining about the lines that I added into the .h file. Having Google'd the first error, and come across this answer on SO: Compiler error C2653: not a class or namespace name , it would appear that the cause is a circular dependency in the header files...
But that is not happening here as far as I can tell... Are there any other reasons why I might get this compile error, and what's preventing my code from building?
The message displayed when I hover my cursor over the first line that I've added in the .h file, Types::ModeRecord is:
Error: a class-qualified name is required
Here is an abbreviated version of the code you posted, with my observation:
class PublisherModule [... bunch of base classes...]
{
public:
Types::ModeRecord;
};
What do you think this line does??
Types::ModeRecord;
It is not of the form [TypeName] [VariableName]; (eg int i;)
It does not look like a method prototype (eg void foo(int i);)
It is not a typedef.
It doesn't look like any normal C++.
What do you think it is doing?
I'm just beginning to write code with C++, and I got stuck when I want to add reference to class from other project from the same solutions.
my Main code located at :
InventoryAppDLL (contains code to access Sql Database)
class : dbConnection.h
Code:
ref class dbConnection
{
public:
dbConnection();
void SetCommandText(String ^command,bool ^commandText);
int ExecuteScalar();
DataSet^ ExecuteDataSet();
DataTable^ ExecuteDataTable();
protected:
~dbConnection();
private:
DbConnection ^conn;
DbCommand ^cmd;
ConnectionStringSettings ^settings;
DbProviderFactory ^fac;
};
after i build the main project (InventoryAppDLL), it was success and contains no errors.
but after I include the header into another project (InventoryAppService),and then I build it, it contains error :
error C2143: syntax error : missing ';' before '^'
when I reference to the error, I've got ConnectionStringSettings missing library on dbConnectionClass (InventoryAppDLL).
private:
DbConnection ^conn;
DbCommand ^cmd;
ConnectionStringSettings ^settings; // <---
DbProviderFactory ^fac;
};
I think you don't even need to include the header file. Since you have already added a reference from the consuming project to the project containing this class, the consuming project can read the metadata from the DLL project and you can simply say:
auto x = ref new dbConnection();
I'm writing a little DirectX model viewer.
I just finished writing my scene manger.
I will only ever have 1 scene manger object so my idea was to create a global pointer of type "scnManger" and then set it when i create my scene manger object.
This would let me get to it from any where in my application.
I'm getting a compile error though:
1>shaderViewer.obj : error LNK2005: "class scnManger * sceneManger" (?sceneManger##3PAVscnManger##A) already defined in sceneManager.obj
1>C:\Users\Greg\Documents\Visual Studio 2010\Projects\ShaderViewer\Debug\ShaderViewer.exe : fatal error LNK1169: one or more multiply defined symbols found
right now I have 3 files
sceneManger.h:
// Our global scene manger variable
scnManger* sceneManger;
shadherViewer.cpp (winMain & includes sceneManger.h):
scnManger shaderViewerScnManger;
sceneManger = &shaderViewerScnManger;
sceneManger.cpp (includes sceneManger.h):
I use the methods of my scene manger object in here for various things.
First I would like to understand why I am getting the error, and am also open to any suggestions about a better way to handle this. I wasn't sure if using a global variable like this was a good idea or not.
You should not define global variables in .h files. You should declare them in .h, like this:
extern scnManger* sceneManger;
and then define them in one cpp file, like this:
scnManger* sceneManger;
Otherwise, every cpp file that includes your .h file will declare the sceneManger variable, resulting in a name collision.
I get confused when I get errors like these
I have
FxSmartPtr<FxStreamable> able(FcNew,stream->StreamInObject());
FxGlobalPair pair(id,able);
I get an error on FxGlobalPair pair(id,able); that is able is not a type.
I tried modifying to
FxGlobalPair pair(id,FxSmartPtr<FxStreamable>::able);
but I get an error that is error: 'class FxSmartPtr<FxStreamable>::able' has not been declared
What concept am I missing?
UPDATE: typedef pair<FxID, FxSmartPtr<FxStreamable> > FxGlobalPair;
UPDATE 2:
Heading
I think that you have found the Most Vexing parse
The problem is that
FxSmartPtr able(FcNew,stream->StreamInObject());
may define a function named able, instead of a variable.