Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Setting A List Type To A Struct. Really Stuck :(

linked list

  • Please log in to reply
4 replies to this topic

#1 kumogoro

kumogoro

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Learning:C++

Posted 06 May 2012 - 02:20 AM

HI guys,
I'm trying to make a snake game as part of my university course. We're using a provided API but what i'm stuck with is getting the linked list, which i will be using for the snake, to accept anything other than a simple type like <int> or <string>. Here is the struct I've made:


typedef struct
{
unsigned int x;
unsigned int y;
}snake_segment, new_head;

and the way i'm trying to use it with the list:


void Snake::MoveDown(list<snake_segment>& snake)
{
new_head;

snake.pop_back();

new_head.x = snake.front().x;
new_head.y = snake.front().y+10;

snake.push_front();
}

at new_head.x we get the error "a nonstatic reference must be relative to a specific object" and at snake.push_front() we get "no instance of overloaded function" I have also added new_head x, y; to the data members of the Snake class which these functions are used in.

Honestly any help would be appreciated greatly! I've been stuck on this issue for for too long! :(
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 06 May 2012 - 02:31 AM

There are two mistakes you made in method MoveDown -- i) you have not declared any variables for type new_head. But you just typed the type 'new_head'. You just need to declare a variable of type new_head and use it. ii) you have passed nothing to call push_front method of list class. This method requires an argument to push on the front. You need to push the variable you have not declared in mistake 1.

Following is the code without compilation errors.

void MoveDown(list<snake_segment>& snake)
{
new_head nh;

snake.pop_back();

nh.x = snake.front().x;
nh.y = snake.front().y+10;

snake.push_front(nh);
}

  • 0

#3 kumogoro

kumogoro

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Learning:C++

Posted 06 May 2012 - 07:28 AM

Thank you! That solved the problem with the linked list :) Honestly, thank you so much, still learning the language so am making pretty silly mistakes >_<
  • 0

#4 kumogoro

kumogoro

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Learning:C++

Posted 06 May 2012 - 10:45 AM

erm, kernelcoder if you would be willing to help me out once more? :/ when calling the function i get the nonstatic member error again:

list<snake_segment> mysnake; //defined in same file as function call

Snake::MoveLeft(mysnake);

I'm so sorry that i'm not very good at C++ yet T^T thank you for having the patience to reply at all.
  • 0

#5 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 06 May 2012 - 05:50 PM

The error is because you are not calling MoveLeft method of class Snake with an instance of Snake.

To call a non-static method of class Snake, you need write like following...
//Way1
Snake snake;
snake.MoveLeft(mysnake);

//Way2
Snake *snake = new Snake()
snake->MoveLeft(mysnake);

  • 0





Also tagged with one or more of these keywords: linked list

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download