0

Please understand that I have a strong fever while writing this, and, in addition, it is years ago since I have used C++ - or classes for that matter.

My problem consists in the g++ compiler rejecting my calling of the class identifier (+ the member specifier), and the actual member of that class.

Here is what I mean:

class window{
public:
int borderX, borderY, menu_item;
};

If I choose to call one of these members (boderX, borderY, menu_item), like so:

window.borderX = [some value here];

I get an error in return:

error: expected unqualified-id before '.' token

When I look at cplusplus' website, this code is NOT grammatically incorrect. Yet, it refuses to compile?

Here is an example from cplusplus' website:

class CRectangle {
    int width, height;
  public:
    void set_values (int, int);
    int area (void) {return (width * height);}
};
CRectangle rect;
  rect.set_values (3,4);

This code does NOT compile either!

It returns this error:

error: 'rect' does not name a type

I don't understand why it returns these errors. Window IS used as an identifier - or a type thereof. And secondly, it won't EVEN compile the code from the very website itself which TEACHES the use of C++.

I am waiting to be corrected on these matters. Also, for the record, I am using MingW. Both Code::Blocks and Netbeans yield the same results (yes I KNOW they're IDEs, and not compilers.)

  • 1
    Your `window` example needs an object. The second needs a main function ([see reproduced compiler error](http://liveworkspace.org/code/147aeaa560062ccb4985245ab9549d9d)). – chris Sep 28 '12 at 20:23
  • 2
    The first with `window` doesn't work because you're accessing non-static data members on a type – you need an _object_ first. The second with `CRectangle` doesn't work because you haven't defined `CRectangle::set_values`. – ildjarn Sep 28 '12 at 20:24
  • @ildjarn, True, though that's a linker error. Better finding it now than after it compiles and just gives a new error, though. – chris Sep 28 '12 at 20:25
  • Actually he has an object :) Its window, he needs an instance of the object. – fonZ Sep 28 '12 at 20:25
  • 1
    @JonathanCruz, That's a class. An instance of a class is an object. – chris Sep 28 '12 at 20:25
  • @Chris hm yea actually you're right :) – fonZ Sep 28 '12 at 20:26
  • The problem you're having with the example is that you need to put it inside a function (the last line for sure, but you can put the whole thing into a function if you want). The last line is a statement, and you can't put statements at namespace scope. – Ben Voigt Sep 28 '12 at 20:33
  • Welcome to stackoverflow. I hope you feel better soon. Part of the reason that you have received two answers that don't satisfy you is that your question is incomplete. Please post a complete minimal program that demonstrates your problem. See http://SSCCE.ORG for more information. – Robᵩ Sep 28 '12 at 20:49
  • A question before I get coding: do I need to close this page since my issue has been resolved--can I do that? Chris, I was in fact experiencing odd linker issues (which I don't think relates to my clumsiness) in Code::Blocks, hence switching to Netbeans. Of course, I realize that switching IDE should not resolve such issues - though it seems to have done so. I have actually defined the set_values function, Ildjarn, but it's done in a header - hence your lack of awareness. Once again, thanks everyone for the kind help. – user1707244 Sep 28 '12 at 21:00
  • @user1707244 If an answer resolves your problem, upvote and click the checkmark next to it to mark it as "accepted". That "closes" your question. – Ben Richards Sep 28 '12 at 21:22

2 Answers2

4

'window' defines the class, but you need to make an instance of that class in order to use it's non-static members and methods.

You can either create an instance on the stack,

window w;
w.borderX = [some value here];

Or create one on the heap,

window *w = new window();
w->borderX = [some value here];
PherricOxide
  • 15,493
  • 3
  • 28
  • 41
  • 2
    But creating one on the heap creates all kinds of *new* (haha) problems if you don't use something with RAII. – chris Sep 28 '12 at 20:28
  • Having to edit again. Don't want to impose an impolite position. But please read properly, the original post, before posting. I already declared how it does not work properly. Again, don't take it ill. – user1707244 Sep 28 '12 at 20:44
  • (Adding a comment since I think this is a good place to start.) @user1707244 Do you have the `window` class defined in a separate file from your `main` function? If so, what do you have for `#include` statements? Also, how are you calling your compiler? – Ben Richards Sep 28 '12 at 20:49
  • Good question. Yes, I do have it in a header, but it's not a problem. I knew that this might have been the problem, so I thought ahead and added my class declaration in my main.cpp. So, to answer your question, yes, I have. – user1707244 Sep 28 '12 at 20:51
  • @user1707244 Ok, then. My suspicion, there, is alleviated. – Ben Richards Sep 28 '12 at 21:19
3

Try this:

First you declare the class. In window.h

#include <iostream> // for cout and endl

class Window{
public:
    int borderX, borderY, menu_item;
};

In your main file where you start the program:

#include <window.h>
int main() {
    // 1. make an instance of Window
    Window w;         

    // 2. set some values
    w.borderX = 12;   
    w.borderY = 8;
    w.menu_item = 7;

    // 3. print the values
    std::cout << "X: "  << w.borderX 
              << " Y: " << w.borderY 
              << " menu item: " << w.menu_item << std::endl;

    //OR with a pointer
    // 1. create a new pointer that points to 
    //    an instance of Window (which is also create in the process)
    Window pw = new Window(); 

    // 3. set some values
    pw->borderX = 9;           
    pw->borderY = 12;
    pw->menu_item = 18;

    // 3. print the values
    std::cout << "X: "<< pw->borderX << " Y: " << pw->borderY 
              << " menu item: " << pw->menu_item << std::endl;

    // 4. Rule: for every *new* (new Window()) there is 1 delete
    //    So every pointer should be deleted somewhere 1 time to avoid memory leaks
    delete pw; 

    return 0;
}

And i hope you get better soon. Having fever is not funny.

fonZ
  • 2,428
  • 4
  • 21
  • 40
  • Does NOT work!!! I already showed how it didn't work in my original post! Sorry for being impolite... Returns: main.cpp:8:1: error: 'window' does not name a type And: main.cpp:9:1: error: 'w' does not name a type – user1707244 Sep 28 '12 at 20:46
  • 1
    FYI: Your `main` has the wrong signature. http://stackoverflow.com/questions/449851/why-do-we-need-to-use-int-main-and-not-void-main-in-c – Robᵩ Sep 28 '12 at 20:47
  • Note that C++ is case sensitive. He's using upper case Window in his example and your error has lowercase "window", so you either typed the error wrong or didn't copy/paste his code exactly. – PherricOxide Sep 28 '12 at 20:47
  • You also need "std::endl" instead of endl; – PherricOxide Sep 28 '12 at 20:49
  • Right, i use uppercase for window because its a convention. Names representing types must be in mixed case starting with upper case. @Robᵩ thanks for pointing it out :) – fonZ Sep 28 '12 at 20:51
  • You're also using w-> instead of pw-> for the pointer to a window references. Compiles after that fix. – PherricOxide Sep 28 '12 at 20:51
  • @user1707244 i assumed you would put it all in 1 file to test it out. I will add a header file in the example. – fonZ Sep 28 '12 at 20:52
  • I am aware of the std namespace, and am also aware of case sensitivity. These two factors do not play a role, as I have already thought ahead of them. – user1707244 Sep 28 '12 at 20:52
  • http://ideone.com/57UEw Compiles fine here, so maybe you're not including the correct files in your ide/build tools? – PherricOxide Sep 28 '12 at 20:53
  • @user1707244 then why dont you use it accordingly? Sorry just trying to help here. This code works. – fonZ Sep 28 '12 at 20:54
  • Yes, Jonathan, it does work. This is a case of having very little sleep plus being sick (lightheaded), and also having forgot the basics of class declaration. It does work now. Thanks everyone for sticking through my clumsiness. – user1707244 Sep 28 '12 at 20:55
  • :) no worries. Glad i(we) could help. – fonZ Sep 28 '12 at 20:56