【问题标题】:"Undefined symbols for architecture arm64" - What does this mean? [duplicate]\"架构 arm64 的未定义符号\" - 这是什么意思? [复制]
【发布时间】:2023-02-11 04:39:55
【问题描述】:

我无法让我的代码运行,互联网似乎也不知道为什么。我不确定我需要让您知道什么,但如果有帮助,我正在使用 CLion。

这是我的 plant.h 文件:

#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

这是我的 plant.cpp 文件:

#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;
}

这是我的 main.cpp 文件:

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

using namespace std;

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

    return 0;
}

这是我的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.21)
project(Coursework)

set(CMAKE_CXX_STANDARD 14)

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

预先感谢您的任何帮助!

【问题讨论】:

  • 请在本网站搜索Undefined symbols for architecture arm64。已有 800 多个现有问题和答案。我们希望您在发帖之前自行解决问题的其中一项工作是彻底搜索。搜索框位于每个页面的顶部 - 请使用它。
  • 我是这个论坛的新手从技术上讲,您不是,因为这不是论坛。这是一个问答网站。在论坛上,您与人们进行讨论。您可以在这里发布问题或答案,也可能是寻求澄清的评论。每一页都是一个且只有一个问题和零个或多个答案。事实上,这个问题对于初学者来说非常好,但我强烈建议您参加tour,如果您还没有阅读How to Ask,以确保您对 SO 有足够的了解以获得良好的用户体验.
  • 您还需要发布您用于编译代码的确切命令,以及完整的错误消息,以便我们可以确切地知道缺少哪些符号。提示:您可以忽略错误的“for architecture arm64”部分,然后只关注确保链接器找到这些符号的定义。
  • 另一项建议:始终提供完整且未经编辑的错误消息。将有很多信息以及“架构 arm64 的未定义符号”,其中一些信息对解决问题非常有帮助。呃...是的。他^说了什么。
  • @user4581301 是的,那是我的错——除了论坛,我不知道还能称呼它什么。对不起!我会查看“如何提问”部分。感谢您的帮助 :)

标签: c++ function class header-files definition


【解决方案1】:

未定义的符号表示符号已声明但未定义。

例如在类定义中你有以下不带参数的成员函数

void addGrowth();

但是后来您定义了一个具有相同名称但现在只有一个参数的函数

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

所以在类定义Plant中声明的函数仍然是undefined。

也没有构造函数的定义

 Plant(string x, int y);

在您提供的代码中。

如果您在标头plant.h中使用标准库中的名称string,那么您需要包含标头&lt;string&gt;

#include <string>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    相关资源
    最近更新 更多