0

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!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Please do a search of this site for `Undefined symbols for architecture arm64`. There are 800+ existing questions and answers already. One of the efforts we expect you to make to solve the problem yourself before posting here is a thorough search. The search box is at the top of every single page - please use it. – Ken White Apr 01 '22 at 23:20
  • 1
    *I'm new to this forum* Technically you aren't because this isn't a forum. It is a Questions and Answers site. On a forum you have a discussion with people. Here you post a question or an answer and maybe a comment looking for clarifications. Every page is one-and-only-one question and zero or more answers. As it is, this question's pretty good for a first timer, but I strongly recommend taking the [tour] and, if you haven't already, reading through [ask] to make sure you know enough about SO to have a good user experience. – user4581301 Apr 01 '22 at 23:22
  • You'd also need to post the exact commands you are using to compile the code, and the full error message so we can know exactly what symbols are missing. Hint: You can ignore the "for architecture arm64" part of the error, and then just focus on making sure the linker finds definitions for those symbols. – David Grayson Apr 01 '22 at 23:23
  • One other recommendation: always provide the full and unedited error message. There will be A LOT of information along with "Undefined symbols for architecture arm64", and some of it will be very helpful in solving the problem. Errrr... Yeah. What He ^ said. – user4581301 Apr 01 '22 at 23:23
  • @user4581301 Yes, that's my bad - I didn't know what else to call it apart from a forum. I'm sorry! I'll check out the How to Ask section. Thank you for your help :) – TheEmpressEllaseen Apr 01 '22 at 23:24
  • @KenWhite I did look through several but most seemed to be about the CMakeLists file not having the correct files listed (which I believe mine do), or the function not being defined (which I thought mine was). I'm sorry for posting if I shouldn't have. – TheEmpressEllaseen Apr 01 '22 at 23:26
  • @DavidGrayson That's a very good point about the error message. Rather silly of me to not think of that! Thank you. – TheEmpressEllaseen Apr 01 '22 at 23:32

1 Answers1

1

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>
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335