Thread: Constructor being called in another constructor compiler error

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    27

    Constructor being called in another constructor compiler error

    Hi again.

    I'm getting a strange compiler error that I just can't figure out how to fix.

    the error reads

    error: no matching function for call to ‘Position::Position()’

    heres some of the offending code

    Code:
    food::food()
        {
            ImageFile("../Assets/node3.tga").load(foodImage_);
        }
    this structure is located in a different file, but is global

    Code:
    struct Position
    {
        int x_;
        int y_;
        Position(int x,int y) : x_(x), y_(y){}
    };
    as you can see, Position is a structure, so there isn't a constructor for it, and it doesn't link into the food constructor in any way.

    what could be causing this?

    If more code is needed to diagnose the problem, just say and I shall provide it

    Thanks

    ES

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There is a constructor for Position in your code
    Code:
      Position(int x,int y) : x_(x), y_(y){}
    but there is no constructor with an empty parameter list Position()...

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    Well I changed the food constructor to this

    Code:
     food::food(int x = 0, int y = 0)
        {
            ImageFile("../Assets/node3.tga").load(foodImage_);
        }
    I'm still getting the same error, but it is progressing further in the compilation process, so i guess that's a start

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    No. Is the food class/struct somehow related to the Position class/struct? If not then changing food will have zero impact on Position. Perhaps you should post more of your code so we can see how food and Position interact...it *looks* like somewhere you are creating an instance of Position like:
    Code:
    Position somePosition;
    This would result in the compiler looking for ctor (constructor) for Position that looks like:
    Code:
    Position::Position()
    {
         // foo
    }
    But w/o more code that is only a guess..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    erm, ok

    here's the header file for the food class

    Code:
    class food {
    
        //Attributes
    
        protected:
    
        int type_;
        Image foodImage_;
        int foodcount_;
        Position foodpositions_[5];
    
    
    
        //Operations
    
        public:
                food(int x , int y);
    
        //checks the food count attribute to see if food can be spawned
        void checkfoodamount();
    
        //sets the position for the newly spawned food
        void setposition(int newx, int newy);
    
        //gets the positions of the food that is currently on the board
        void getpositions();
    
        //adds a piece of food
        void draw(Canvas &canvas);
    
    };
    Food does interact with position as there is an array of 5 position structures used to keep track of the food coordinates
    Last edited by elsparko; 05-18-2010 at 08:58 AM.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    problem solved

    I added default values to the position structure and now it works like a charm

    thanks for all the help everybody

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    OK. But can you explain why that worked? If you can't, you have been lucky, and have not actually solved the problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM