Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

error: invalid use of non-static member function

–1 vote
asked Oct 15, 2019 by dyaminvanek314 (170 points)

I'm creating a battleship game, and I want to create a class that I can use for each type of ship (i.e. destroyer, battleship, etc.). The class (Ship) is fine itself, but when I try to call member functions, I get the error message "error: invalid use of non-static member function".  I'm pretty sure it has to do with an error with the way I declared the class or member functions.

Here's the relevant portion of the code if it helps (I know it's probably not very efficient, but I'm not done with it yet anyway).

#include <iostream>
#include <string>
#include <sstream>

//classes
class Ship 
{
    std::string name;
    int length;
    int stype;
    
    public:
    Ship(int length1, std::string name1, int stype1);
    std::string get_name();
    int get_len();
    int get_type();
};

Ship::Ship(int length1, std::string name1, int stype1)
{
    name = name1;
    length = length1;
    stype = stype1;
}

std::string Ship::get_name()
{
    return name;
}

int Ship::get_len()
{
    return length;
}

int Ship::get_type()
{
    return stype;
}

//functions
void setup();
int ship_place(std::string shiptype, std::string shiplen, int ship, int length);

//global variables

Ship destroyer(2,"Destroyer",1);
Ship submarine(3,"Submarine",2);
Ship cruiser(3,"Cruiser",3);
Ship battleship(4,"Battleship",4);
Ship carrier(5,"Carrier",5);

int main()
{
    setup();
    return 0;
}

void setup()
{
    std::cout << "I-am-setupbot.You-are-required-to-place-all-five(5)-of-your-ships-on-your-board." << std::endl;
    ship_place(destroyer.get_name, "1x2", destroyer.get_type, destroyer.get_len);
    ship_place(submarine.get_name, "1x3", submarine.get_type, submarine.get_len);
    ship_place(cruiser.get_name, "1x3", cruiser.get_type, cruiser.get_len);
    ship_place(battleship.get_name, "1x4", battleship.get_type, battleship.get_len);
    ship_place(carrier.get_name, "1x5", carrier.get_type, carrier.get_len);

    // an error gets thrown on all 5 of the above lines for (shipvariable).get_name . When I remove the first portion, an error gets thrown for the get_type as well, as would the get_len.
}

//function I use to set the positions of the 5 ships (not important).

int ship_place(std::string shiptype, std::string shiplen, int ship, int length)
{
  
    
   //had to delete stuff b/c too many characters

}

Here's the error messages:

main.cpp: In function ‘void setup()’:
main.cpp:159:80: error: invalid use of non-static member function ‘std::string Ship::get_name()’
     ship_place(destroyer.get_name, "1x2", destroyer.get_type, destroyer.get_len);
                                                                                ^
main.cpp:26:13: note: declared here
 std::string Ship::get_name()
             
main.cpp:160:80: error: invalid use of non-static member function ‘std::string Ship::get_name()’
     ship_place(submarine.get_name, "1x3", submarine.get_type, submarine.get_len);
                                                                                ^
main.cpp:26:13: note: declared here
 std::string Ship::get_name()
             
main.cpp:161:74: error: invalid use of non-static member function ‘std::string Ship::get_name()’
     ship_place(cruiser.get_name, "1x3", cruiser.get_type, cruiser.get_len);
                                                                          ^
main.cpp:26:13: note: declared here
 std::string Ship::get_name()
             
main.cpp:162:83: error: invalid use of non-static member function ‘std::string Ship::get_name()’
     ship_place(battleship.get_name, "1x4", battleship.get_type, battleship.get_len);
                                                                                   ^
main.cpp:26:13: note: declared here
 std::string Ship::get_name()
main.cpp:163:74: error: invalid use of non-static member function ‘std::string Ship::get_name()’
     ship_place(carrier.get_name, "1x5", carrier.get_type, carrier.get_len);
                                                                          ^
main.cpp:26:13: note: declared here
 std::string Ship::get_name()
If anybody could help, it would be sincerely appreciated.

1 Answer

0 votes
answered Oct 17, 2019 by frank

You typed the method name without brackets e.g destroyer.get_name instead of destroyer.get_name()

commented Oct 17, 2019 by dyaminvanek314 (170 points)
@frank thank you! Works perfectly!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...