Search code examples
c++functionclassheader-filesdefinition

"Undefined symbols for architecture arm64" - What does this mean?


I'm unable to get my code to run, and the internet doesn't seem to know why. I'm not sure what I need to let you know, but I am using CLion if that helps.

This is my plant.h file:

#ifndef COURSEWORK_PLANT_H
#define COURSEWORK_PLANT_H

using namespace std;

class Plant {
public:
    void addGrowth();
    int getSize();
    string getName();
    Plant(string x, int y);
private:
    string plantName;
    int plantSize;
};

#endif //COURSEWORK_PLANT_H

This is my plant.cpp file:

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

using namespace std;

void Plant::addGrowth(int x) {
    plantSize += x;
    cout << "You have added " << x << " leaves to your plant. Well done!";
}

int Plant::getSize() {
    return Plant::plantSize;
}

string Plant::getName() {
    return Plant::plantName;
}

This is my main.cpp file:

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

using namespace std;

int main() {
    Plant myPlant("Bugg", 2);

    return 0;
}

This is my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.21)
project(Coursework)

set(CMAKE_CXX_STANDARD 14)

add_executable(Coursework main.cpp plant.h plant.cpp)

Thank you in advance for any help!


Solution

  • Undefined symbol means that a symbol is declared but not defined.

    For example in the class definition you have the following member function without parameters

    void addGrowth();
    

    But then you defined a function with the same name but now with one parameter

    void Plant::addGrowth(int x) {
        plantSize += x;
        cout << "You have added " << x << " leaves to your plant. Well done!";
    }
    

    So the function declared in the class definition Plant is still undefined.

    Also there is no definition of the constructor

     Plant(string x, int y);
    

    in the provided by you code.

    And if you are using the name string from the standard library in the header plant.h then you need to include the header <string>

    #include <string>