C ++的新手,未定义的引用

时间:2017-06-29 19:26:17

标签: c++ qt object kdevelop

通常我在java中编程,在C中也有一些基本概念(头文件,make文件,函数,指针,用C编程和使用汇编函数[在大学里],forks,threads,semaphores,pipes)。

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

主要是我调用类:Categoty和CategoryList。

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

问题:

代码给出了一些错误:"未定义的引用"基本上是主要的。

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

我想简单地创建一个列表,并在该列表中添加一个元素。

代码:

CategoryList标题:

#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

CategoryList 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)

1 个答案:

答案 0 :(得分:1)

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

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