Hello,

Sorry if this is a basic basic question.

I am familiar with doing console projects, but I am trying to do a gui project and the rules just don't seem to be the same.

I have two basic questions.
1) Why can I not include fstream and/or string headers in my gui project?
2) Why can I not instantiate variables outside of the WindowProcedure or the WinMain functions?

Below is some code with the error messages i received as comments.

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include "my.cpp"

typedef struct county
{
    int nType;
};
class CYo {
  public:
    int xyz;
};

int someint;
CYo bob;
CYoo alex;//defined in my.cpp
county coul = {0};//no error
string somestr;//error: `string' does not name a type
fstream myfile;//error: `fstream' does not name a type

someint=3;//error: expected constructor, destructor, or type conversion before '=' token
bob.xyz = 4;//error: expected constructor, destructor, or type conversion before '.' token
alex.xyz = 5;//error: expected constructor, destructor, or type conversion before '.' token
coul.nType = 6;//error: expected constructor, destructor, or type conversion before '.' token


LRESULT CALLBACK WindowProcedure {...};//header
int WINAPI WinMain (..) {
	//standard window class and message pump
}

LRESULT CALLBACK WindowProcedure () {
  someint = 3;//no error
  bob.xyz = 4;//no error
  alex.xyz = 5;//no error
  coul.nType = 6;//no error
  //standard msg switch
}

The compiler does not complain about not being able to find the fstream/string headers, but yet it complains about not knowing the data type.
The compiler does not complain about my included data types.
The compiler does not like 'global' instantiation.

What is going on here?

Thanks,

I have two basic questions.
1) Why can I not include fstream and/or string headers in my gui project?
2) Why can I not instantiate variables outside of the WindowProcedure or the WinMain functions?

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include "my.cpp"

// since fstream and string are in the 'std' namespace 
// use the following ...
using namespace std;
// or otherwise you'll have to specify 'std' explicitly, i.e.
std::string astring = "something";
std::ifstream somestream;

// a global variable, the wrong way ...
int someint;
someint=3;//error: expected constructor, destructor ...
// another global variable, they have to be initialized at once ...
int someotherint = 123;

If you #include "my.cpp" then you must not compile and link the "my.cpp" along with the project, or your linker will complain about multiple definitions. I think you should change that so that you write a respective header file and include it instead of "my.cpp" i.e. you'd have #include "my.h" .

So all in all, the errors are not related to a GUI program in any way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.