?
Solved

Error      1      error C2039: 'x' : is not a member of "

Posted on 2010-04-08
15
Medium Priority
?
628 Views
Last Modified: 2013-12-21
I am getting this error when I try to return the x and y positions of the sub I have posted my class and the code used to call along with the full error message, thanks to all who look.

Quick bit of background, the ints will be used as the coordinates for a sprite.

class Submarine {

private:

      static int subPosX;
      static int subPosY;

      bool powerEmpty;
      int power;

      int distance;

public:

      int setdistance(int);

      void setX(int x)
      {
            subPosX = x;
      }
      void setY(int y)
      {
            subPosY;
      }
      int GetX()
      {
            return subPosX;
      }
      int GetY()
      {
            return subPosY;
      }

This is how I am calling the ints

      static int x;
      static int y;
      //Submarine
      Submarine Lead;
      Lead.setX(150);
      Lead.setY(300);

      x = Lead.GetX();
      y = Lead.GetY();

This is the full error
Error      1      error C2039: 'x' : is not a member of 'Submarine'      c:\users\krups\desktop\uni\sub_game\sub\csci2605cwk\mainscroller3.cpp      58

0
Comment
Question by:gras5hopper
  • 8
  • 7
15 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 30138040
One thing I noticed :

>>       void setY(int y)
>>       {
>>             subPosY;
>>       }

You're not setting subPosY here ;)

Regarding the error you're having : could you indicate which line that error refers to ? Could you also post the entire mainscroller3.cpp file ?
0
 

Author Comment

by:gras5hopper
ID: 30139435

Thanks missed that:)

The error is at line:
      static int x;
      static int y;
      //Submarine
      Submarine Lead;
      Lead.setX(150);
      Lead.setY(300);
      Lead.
//Lead.GetX = x;
      //Lead.GetY = y;
>>      x = Lead.GetX();
      y = Lead.GetY();
I get no error for y :\ not sure why?

This is a project that I am doing I am using SFML.

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <sstream>
#include "Sub3.h"
using namespace std;

int main()
{


      //window dimensions
      unsigned int width=600;
      unsigned int height=480;
      
      //we need a timer to update things - get a clock
      sf::Clock clock;
      static float timeAccumulator=0.0;

      //load resources for the scroller - images and sounds (if you have any)      
      sf::Image backGround;
      if(!backGround.LoadFromFile("images/background.jpg")) std::cout<<"File not found background.jpg"<<std::endl;
      
      //Load sub
            sf::Image subSprite;      
      if(!subSprite.LoadFromFile("images/subpng.png")) std::cout<<"File not found rock.png"<<std::endl;

      //resouces loaded

      sf::Sprite backGndSprite(backGround);
      backGndSprite.Scale(0.75f,0.8f);
      static int x;
      static int y;
      //Submarine
      Submarine Lead;
      Lead.setX(150);
      Lead.setY(300);
      Lead.
      //Lead.GetX = x;
      //Lead.GetY = y;
      x = Lead.GetX();
      y = Lead.GetY();

// Sub
      sf::Sprite playerSub(subSprite,Vector2f(x,y),Vector2f(2,2),0,Color(255,255,255,255));



      // Create main window
    sf::RenderWindow App(sf::VideoMode(width, height), "Scroller Window");
      
    // Start the display loop
    while (App.IsOpened())
    {
            sf::Event Event;
        static int speed=1; //remembers its value through the loop
            //check inputs in the event queue
                         while (App.GetEvent(Event))
                        {
                              // Close window : exit
                              if (Event.Type == sf::Event::Closed)
                                    App.Close();
                              // Escape key : exit
                              if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                                    App.Close();
      
                              }
                              //Left arrow key :
                              if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
                              {
                                    
                                    cout<<"the x val is "<<Lead.GetX();

                              }
      
                              //other events are ignored
                        }


                         //update the things that need updating in the game loop
                        //update speed
                         float f=clock.GetElapsedTime();
                         clock.Reset();
                         timeAccumulator+=f;
                         scr.update(f*speed);

                         

                         //draw the things you have to draw
                         App.Draw(backGndSprite);
                         App.Draw(scr);      
                          App.Draw(playerSub);


                  
        // Finally, display the rendered frame on screen
        App.Display();
    }

   
    return EXIT_SUCCESS;

}
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 30139659
>>       Lead.
>> //Lead.GetX = x;
>>       //Lead.GetY = y;
>> >>      x = Lead.GetX();

Do you notice the first line in the above quote ? It should not be there (likely it was left by earlier edits or so).
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:gras5hopper
ID: 30139874
thats an accident sorry...
after removing it I am getting these 3 errors
Error      4      error LNK2001: unresolved external symbol "private: static int Submarine::subPosX" (?subPosX@Submarine@@0HA)      mainScroller3.obj
Error      5      error LNK2001: unresolved external symbol "private: static int Submarine::subPosY" (?subPosY@Submarine@@0HA)      mainScroller3.obj
Error      6      fatal error LNK1120: 2 unresolved externals      C:\Users\Krups\Desktop\Uni\Sub_Game\Sub\\csci2605Cwkdebug.exe

:S I mean I've left somthing out of place before but now I am just confused...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 30140064
Your code tries to call methods of the Submarine function called subPosX and subPosY. But there are no such methods. You have methods named setX, setY, GetX and GetY though - you probably meant to call some of those instead.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 30140157
Scratch that. I just noticed that you declared the subPosX and subPosY members static :

>>       static int subPosX;
>>       static int subPosY;

That should probably not be the case. Just remove the static keywords there.
0
 

Author Comment

by:gras5hopper
ID: 30140458
"Your code tries to call methods of the Submarine function called subPosX and subPosY. But there are no such methods. You have methods named setX, setY, GetX and GetY though - you probably meant to call some of those instead."

Apologies I don't quite follow...

Also I removed the statics after I searched using the error code. at the moment is seems as if the functions are working as the submarine renders at 300,150. I need to set up a way to move them, which I presume ill use the setX and some key events?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 30140600
>> Apologies I don't quite follow...

Ignore that - I didn't read the error message properly. The correct solution is explained in http:#30140157.


>> at the moment is seems as if the functions are working

Looks great. So, your issue is resolved then ?
0
 

Author Comment

by:gras5hopper
ID: 30141022
If you could help me with one more function and ask set 500 points for your reply I will award it, but I would like to ask one more question as you are very helpful and I'd rather not get messed up again, Thanks in advance.

I added the function setXY to the class and I want to use key events to change the x/y of the sub
*updated class*
class Submarine {

private:

       int subPosX;
       int subPosY;

      bool powerEmpty;
      int power;
      
      int distance;

public:

      int setdistance(int);

      void setX(int x)
      {
            subPosX = x;
      }
      void setY(int y)
      {
            subPosY = y;
      }
      int GetX()
      {
            return subPosX;
      }
      int GetY()
      {
            return subPosY;
      }
      void setXY(int, int)
      {
            subPosX;
            subPosY;
      }

and the keyevent:

                              if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
                              {
                                    //playerSub.Move(-3.0,0);
                                    Lead.setXY(-50.0,0.0);
                                    cout<<"the x val is "<<Lead.GetX();
                                    cout<<"the y val is "<<Lead.GetY();
                                        }

however the subPosX and subPosY do not seem to change and the sprite does not move :S

Thanks again
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 total points
ID: 30141258
>>       void setXY(int, int)
>>       {
>>             subPosX;
>>             subPosY;
>>       }

As before, you need to actually assign something to subPosX and subPosY. you'll also want to name the two parameters (x and y resp. eg.).


Note also that :

>>                                     Lead.setXY(-50.0,0.0);

-50.0 and 0.0 are double's, not ints.
0
 

Author Closing Comment

by:gras5hopper
ID: 31712527
Really helpful guy
0
 

Author Comment

by:gras5hopper
ID: 30141605
still can't set x/y with Lead.setXY(50,0);
they are not staying the same every game loop i think:S
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 30141830
Well, every time the left key is pressed, you set the position to the same exact position.

What you probably meant to do, is subtract 50 from the x position. Something like :

        Lead.setX(Lead.GetX() - 50);

or better yet, create a separate method to implement moves, like you had in the commented line of code :

>>                                     //playerSub.Move(-3.0,0);
0
 

Author Comment

by:gras5hopper
ID: 30142101
Thats a good idea, thank yo uvery much for all your help. =]
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 30142179
No worries :)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Joseph
CONTENTS I.      What is a MOBA?   a. Definition   b. Origin   c. Current Generation II. The New Games   a. League of Legends   b. Heroes of Newerth   c. Dota 2   d. Smite III. Comparison IV. Conclusion ------------------------…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question