gpt4 book ai didi

c++ - C++ 新手, undefined reference

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:01 26 4
gpt4 key购买 nike

通常我用 java 编程,也有一些 C 的基本概念(头文件、make 文件、函数、指针、C 编程和使用汇编函数[在大学]、fork、线程、信号量、管道)。

我正在尝试用 C++ 创建一个简单的程序来添加到我的 git 存储库并增加经验。

基本上在主要的类中调用:Categoty 和 CategoryList。

一个 CategoryList 可以有零个或多个 Category 对象。

问题:

代码给出了一些错误:主要是“ undefined reference ”。

CategoryList *cat = new CategoryList();
Category *c = new Category("Banana","Sobre bananas");
cat->addCategory(*c);

我只想创建一个列表,然后向该列表添加一个元素。

代码:

类别列表标题:

#ifndef CATEGORYLIST_H
#define CATEGORYLIST_H

#include <./src/Domain/Category.h>
#include <set>

using namespace std;


class CategoryList{

protected:
/**
* CATEGORY LIST
**/
set<Category> categorySet;


public:
CategoryList();
CategoryList(set<Category> c);
void addCategory(Category c);
int main();

private:


};

#endif // CATEGORYLIST_H

类别列表 Cpp:

#include "categorylist.h"


CategoryList::CategoryList(){
this->categorySet = set<Category>();
}

CategoryList::CategoryList(set<Category> list){
this->categorySet = list;
}


void CategoryList::addCategory(Category c){
this->categorySet.insert(c);
}

类别标题:

#ifndef CATEGORY_H
#define CATEGORY_H


#include <string>

using namespace std;

class Category{

protected:
string name;
string description;


public:
Category(string name, string description);


private:


};

#endif // CATEGORY_H

类别 Cpp:

#include "Category.h"


/**
*
* CONSTRUCTOR
*
**/
Category::Category(string name, string description){
this->name = name;
this->description = description;
}

主要cpp:

#include "firstcplusplusqt5program.h"
#include <QApplication>
#include <iostream>
#include <string>

#include "List/categorylist.h"
#include "Domain/Category.h"

using namespace std;

void simpleTest();

int main(int argc, char *argv[]){

simpleTest();

QApplication app(argc, argv);
FirstCplusPlusQt5Program w;
w.show();

return app.exec();

}


/**
*
* USE THIS FUNCION TO TESTE SIMPLE FUNCTIONS ON STARTUP
*
**/
void simpleTest(){
cout << "Welcome to José Rodrigues first cpp program." << endl;
for (int i = 0 ; i < 3 ; i++){
cout << "Count " << i << endl;
}

CategoryList *cat = new CategoryList();
Category *c = new Category("Banana","Sobre bananas");
cat->addCategory(*c);


}

我使用的 IDE(Kdevelop、Netbeans、QT Creator)在此配置中没有显示任何错误。所以只有编译器给出错误:

/home/arch-admin/Git/FirstCplusPlusQtApp/FirstCplusPlusQt5Program/build> /usr/bin/ninja firstcplusplusqt5program
[1/4] Automatic MOC for target firstcplusplusqt5program
[2/3] Building CXX object CMakeFiles/firstcplusplusqt5program.dir/src/main.cpp.o
[3/3] Linking CXX executable firstcplusplusqt5program
FAILED: firstcplusplusqt5program
: && /usr/bin/c++ -g -rdynamic CMakeFiles/firstcplusplusqt5program.dir/src/main.cpp.o CMakeFiles/firstcplusplusqt5program.dir/src/firstcplusplusqt5program.cpp.o CMakeFiles/firstcplusplusqt5program.dir/firstcplusplusqt5program_autogen/moc_compilation.cpp.o -o firstcplusplusqt5program /usr/lib/libQt5Widgets.so.5.9.0 /usr/lib/libQt5Gui.so.5.9.0 /usr/lib/libQt5Core.so.5.9.0 && :
CMakeFiles/firstcplusplusqt5program.dir/src/main.cpp.o: In function `simpleTest()':
/home/arch-admin/Git/FirstCplusPlusQtApp/FirstCplusPlusQt5Program/build/../src/main.cpp:37: undefined reference to `CategoryList::CategoryList()'
/home/arch-admin/Git/FirstCplusPlusQtApp/FirstCplusPlusQt5Program/build/../src/main.cpp:38: undefined reference to `Category::Category(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/arch-admin/Git/FirstCplusPlusQtApp/FirstCplusPlusQt5Program/build/../src/main.cpp:39: undefined reference to `CategoryList::addCategory(Category)'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
*** Failure: Exit code 1 ***

项目结构(src 只是因为大小):

`-- src
| |-- Controller
| |-- Domain
| | |-- Author.cpp
| | |-- Author.h
| | |-- Category.cpp
| | |-- Category.h
| | |-- annotation.cpp
| | `-- annotation.h
| |-- List
| | |-- categorylist.cpp
| | `-- categorylist.h
| |-- PlantUmlGenerator
| |-- UI
| |-- Utils
| | |-- utils.cpp
| | `-- utils.h
| |-- firstcplusplusqt5program.cpp
| |-- firstcplusplusqt5program.h
| |-- firstcplusplusqt5program.ui
| `-- main.cpp

会不会是makefile?

在Qt项目中有一个文件CmakeList.txt,我是否应该为依赖修改这个配置文件?

提前致谢。

CmakeList.txt

cmake_minimum_required(VERSION 2.8.11)
project(FirstCplusPlusQt5Program)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Widgets REQUIRED)

set(firstcplusplusqt5program_SRC
src/main.cpp
src/firstcplusplusqt5program.cpp
)

# Create code from a list of Qt designer ui files.
#set(CMAKE_AUTOUIC ON) # use this if you have CMake 3.x instead of the following
qt5_wrap_ui(firstcplusplusqt5program_SRC src/firstcplusplusqt5program.ui)

# Tell CMake to create the helloworld executable
add_executable(firstcplusplusqt5program ${firstcplusplusqt5program_SRC})

# Use the Widgets module from Qt 5.
target_link_libraries(firstcplusplusqt5program Qt5::Widgets)

# Install the executable
install(TARGETS firstcplusplusqt5program DESTINATION bin)

最佳答案

您需要将所有 .cpp 文件添加到您的 CMakeList.txt

...
set(firstcplusplusqt5program_SRC
src/main.cpp
src/firstcplusplusqt5program.cpp
src/List/categoryList.cpp
src/Domain/Category.cpp
...
)
...

关于c++ - C++ 新手, undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44833438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com