Thread: GUI Type Scope and Instantiation

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    GUI Type Scope and Instantiation

    GUI Type Scope and Instantiation

    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:
    #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,

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include "my.cpp"
    Never include .cpp files. Only .h files.

    Code:
    typedef struct county
    Remove typedef.

    Code:
    string somestr;//error: `string' does not name a type
    fstream myfile;//error: `fstream' does not name a type
    Likely missing "std::".
    Code:
    std::string somestr;
    std::fstream myfile;
    Code:
    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
    Code cannot appear outside functions.


    Rules are not different from console projects at all. You are merely doing it a bit wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    typedef allows you to define the data type name. I use it all the time.

    Try

    Code:
    typedef struct countyTag
    {
        int nType;
    } county;
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by novacain View Post
    typedef allows you to define the data type name. I use it all the time.
    Not required in C++.
    All you have to do is:

    Code:
    struct country
    {
        int nType;
    };
    
    country mycountry;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template function instantiation
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 03-09-2008, 06:35 AM
  2. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  3. help, templates and hashing, and yucky stuff like that
    By DarkDays in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2001, 06:01 AM