GUI Type Scope and Instantiation

All questions regarding Windows programming, post here. API,COM, ActiveX, DirectX, OpenGL, MFC and so on...

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

GUI Type Scope and Instantiation

Postby n00b3 » Wed Jun 11, 2008 7:34 pm

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.

Code: Select all
#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,
n00b3
 
Posts: 7
Joined: Fri May 30, 2008 6:25 pm

Postby MXP » Wed Jun 11, 2008 8:55 pm

The rules are the same, your code just does adhere to them.

The things inside fstream, iostream, string, etc. live within the std namespace. You'll want to use std::string somestr; and std::fstream myfile; to create objects of those types.
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm


Return to Windows Programming

Who is online

Users browsing this forum: Google [Bot] and 1 guest