Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:13:24+00:00 2026-06-18T07:13:24+00:00

So, I’m trying to compile my code, but the compiler keeps complaining that ‘mysnake’

  • 0

So, I’m trying to compile my code, but the compiler keeps complaining that “‘mysnake’ undeclared (first use this function)”, but I declared it.
This is my Main.cpp, wehre it is declared.

#include "Class.h"
#include "Snake.h"

int main(int argc, char* args[]){
    Prog run;
    if((run.Init())==false){
      return(1);
      }
    Snake mysnake;
    if(run.LoadFiles()==false){
      return(1);
      }
    run.MainLoop();
    if(run.Draw()==false){
      return(1);
      }
    run.CleanUp();
    return(0);
}

And this is the file that makes the compiler complain (AFAIK it’s the first file with any reference to ‘mysnake’ that gets compiled)

#include "Class.h"
#include<sstream>
#include "Snake.h"

bool Prog::Draw(){
     std::stringstream message;
     SDL_Rect position;
     SDL_BlitSurface(image, NULL, screen, NULL);
     int s=mysnake.EndSnake();
     message<<"Your snake was "<<s<<" blocks long.";
     msg=TTF_RenderText_Solid(font, message.str().c_str(), font_color);
     if(msg==NULL){
       return(false);
       }
     position.x=(WWIDTH-msg->w)/2;
     position.y=(WHEIGHT-msg->h)/2;
     SDL_BlitSurface(msg, NULL, screen, &position);
     SDL_Flip(screen);
     return(true);
     }

I have thought about it for over an hour and I still can’t understand why it does this. By the way I’m using Bloodshed Dev C++
I’d be very grateful for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:13:26+00:00Added an answer on June 18, 2026 at 7:13 am

    Inside your Draw function there is no variable declared called mysnake. That function can’t see the mysnake that’s declared in main because it is local to main. You need to pass your mysnake object to the Draw function so that it knows which snake you’re actually talking about.

    To do that, give Draw an argument of type const Snake&, a “reference to const Snake” (or take away the const if EndSnake is a non-const member function):

    bool Prog::Draw(const Snake& snake) {
      // ...
    }
    

    And when you call Draw in main, do this:

    run.draw(mysnake);
    

    Now your Draw function has a variable called snake which was passed in from main. Because the argument is a reference, the Snake object that it sees is exactly the same object as in main. If the argument had been of type Snake instead of const Snake&, then you would get a copy of the mysnake from main.


    Some extra advice:

    We usually write conditions like (run.Init())==false as just !run.init() – it reads much better. Returning is also usually written as return true;, rather than return(true);, but that’s up to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.