Skip to main content
gamedev.net
🔒 Locked

Question about initializing strings !

Started by pghTech Sep 12, 2006 at 2:59 PM 0 replies 800+ views
Original Post
pghTech
pghTech
I received the following error ( see below) in VC2005, when I tried to initialize the CString that I added in the header file. But don't received the error when I initialize it the .cpp file. THIS DOESN"T WORK

SourceSnipITView.h

...
...
CString AfxMsg;
AfxMsg = "message goes here";

SourceSnipITView.cpp

...
...
AfxMessageBox(AfxMsg, MB_OKCANCEL);

THIS DOES WORK:


SourceSnipITView.h

public:
    CString AfxMsg;


SourceSnipITView.cpp

AfxMsg = "message goes here"
AfxMessageBox(AfxMsg, MB_OKCANCEL);

I get the following errors if I do the first setup:

------ Build started: Project: SourceSnipIT, Configuration: Debug Win32 ------
Compiling...
SourceSnipITView.cpp
c:\documents and settings\c00smm\my documents\visual studio 2005\projects\sourcesnipit\sourcesnipit\sourcesnipitview.h(74) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\c00smm\my documents\visual studio 2005\projects\sourcesnipit\sourcesnipit\sourcesnipitview.h(74) : error C2864: 'CSourceSnipITView::AfxMsg' : only static const integral data members can be initialized within a class
Generating Code...
Compiling...
SourceSnipIT.cpp
c:\documents and settings\c00smm\my documents\visual studio 2005\projects\sourcesnipit\sourcesnipit\sourcesnipitview.h(74) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\c00smm\my documents\visual studio 2005\projects\sourcesnipit\sourcesnipit\sourcesnipitview.h(74) : error C2864: 'CSourceSnipITView::AfxMsg' : only static const integral data members can be initialized within a class
Generating Code...
Build log was saved at "file://c:\Documents and Settings\c00smm\My Documents\Visual Studio 2005\Projects\SourceSnipIT\SourceSnipIT\Debug\BuildLog.htm"
SourceSnipIT - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Lastly, can anyone tell me where I would find the returned value of AfxMessageBox is? I know it's an int, but thought originally it would be a Sting (i.e MB_OK).
MrAccident
MrAccident
You're getting those compilation errors because C++ does not allow you to initialize member variables inside a class definition. As the error message says, only static integral constants can be initialized in this manner.

For example, this is allowed:
class Foo{   static const int myConst = 42;   // some other class stuff};
For member variables, though, you must initialize those somewhere in the code of your class; either in a member function, or in a constructor initialization list.


As for AfxMessageBox, it returns an integer. There are a set of #defined macros you can use to check the return value; depending on the style of message box, it could be IDOK, IDCANCEL, IDYES, IDNO, and a couple of others. You can check return values like this:
int return = AfxMessageBox("?", MB_YESNO);if(return == IDYES){   // user clicked "Yes" button}else if(return == IDNO){   // user clicked "No" button}


MB_OK and MB_YESNO are not strings, they are more #defined macros. They're just integers representing message box styles that you can pass to AfxMessageBox() to specify what buttons you want the message box to have.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.