1

For my class assignment, we had to make a C++ program using a class and then compile it. These are the following.

Snake.h

#ifndef SNAKE_H
#define SNAKE_H

#include <iostream>
using namespace std;

// We need to include string since we need to access it
#include <string>
using std::string;
// Our declaration of our class
class Snake
{
public:
    // These are our properties
    string color;
    double length;
    bool venomous;

    // Our two constructors, this is basically saying the snake is able to create these datatypes.
    // It can either have a default data used or inputted data when called.
    Snake()
    {
    }
    // This one has the parameters
    Snake(string color, double length, bool venomous)
    {
    }

    // Our functions that we are declaring so we can use them in snake.cpp
    // These aren't declared here because we don't have any data yet that they can use
    // We also are just telling the code that they are here and that they may or
    // may not be called at some point

    void display();
    void bite();
};

#endif

Snake.cpp

#include <iostream>
#include "Snake.h"

// These creates our data from the header file.
// Snake is seen below with the default data and then Snake with the parameters can be seen.
Snake::Snake()
{
    color = "Red";
    length = 12.5;
    venomous = false;
}

Snake::Snake(string newColor, double newLength, bool newVenomous)
{
    color = newColor;
    length = newLength;
    venomous = newVenomous;
}

// These initalize these functions so that they can be used when we do our dot callback in our main.
void Snake::bite()
{
    cout << "This snake bites because he is hungry";
}

void Snake::display()
{
    cout << "The color of the snake is: " << color;
    cout << "The length of the snake is: " << length;
    cout << "Is the snake venomous: " << venomous;
}

main.cpp

#include <iostream>
#include "Snake.h"
using namespace std;

int main()
{
    // We create our first snake
    Snake snake1;
    // Now we need to call method that our object contains
    snake1.display();
    snake1.bite();

    // UI is user input
    bool UIVenomous;
    string UIColor, isVenomous;
    double UILength;

    cout << "What color is the snake?" << endl;
    cin >> UIColor;
    cout << "How long is the snake? " << endl;
    cin >> UILength;
    cout << "Is the snake venomous? (y/n) " << endl;
    cin >> isVenomous;
    if (isVenomous == "y")
    {
        UIVenomous = true;
    }
    else if (isVenomous == "n")
    {
        UIVenomous = false;
    }

    // We now call our class and set our object with the variables from above
    Snake snake2(UIColor, UILength, UIVenomous);
    // We need to now call our methods that our object has
    snake2.display();
    snake2.bite();
}

I am using VSCode and when I try to run build task it gives me this error

C:\Users\viens\AppData\Local\Temp\cclzLMka.o: In function `main':
c:/Users/viens/OneDrive/Documents/SchoolWork/Lab3/main.cpp:10: undefined reference to `Snake::display()'
c:/Users/viens/OneDrive/Documents/SchoolWork/Lab3/main.cpp:11: undefined reference to `Snake::bite()'
c:/Users/viens/OneDrive/Documents/SchoolWork/Lab3/main.cpp:36: undefined reference to `Snake::display()'
c:/Users/viens/OneDrive/Documents/SchoolWork/Lab3/main.cpp:37: undefined reference to `Snake::bite()'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
15
  • What compiler are you using with VS code? Most likely you are just compiling main.cpp and not Snake.cpp, hence it can't find the definition for this functions Commented Jun 3, 2020 at 17:05
  • 2
    You need to compile all files and not just main.cpp. If you compile only main.cpp then #include "Snake.h" will still work (main.cpp will "see" the signatures in the Snake class) and main.cpp compiles just fine, but when generating the executable the compiler needs to link everything together and at that stage it does not find the implementations of the methods in the Snake class. Thus you get undefined reference erros during the linking. Commented Jun 3, 2020 at 17:15
  • 1
    Your bug is most likely in your tasks.json file Commented Jun 3, 2020 at 17:19
  • 2
    Visual Studio Code is a tricky beast to wrangle. I do not recommend it to people trying to learn C++ because they wind up having to learn two things and can't effectively learn the important one, C++, until after they've learned care and feeding of Visual Studio Code. This may change in a few years , but for now you're probably better off with a pre-configured IDE like Visual Studio. Commented Jun 3, 2020 at 17:25
  • 1
    Related: https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files Commented Jun 3, 2020 at 17:48

2 Answers 2

0

It compiled regularly after I removed the bodies of constructors in the header file.

/* Snake.h
*/
Snake();
Snake(string color, double length, bool venomous);
Sign up to request clarification or add additional context in comments.

Comments

0

If there's an undefined reference error, usually it's because the .o file (which gets created from the .cpp file) doesn't exist and your compiler/build system is not able to link it.

You can use this in CLI: g++ main.cpp other.cpp etc.cpp

1 Comment

I dunno about usually. It looks to be true for this case, but a lot of the time it's a silly typo. or the prototype not matching the implementation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.