对 class 的未定义引用,常见答案尚未解决

Undefined reference to class, common answers haven't resolved

我确定这是一个常见问题,我一直在寻找类似的问题;但我无法解决这个问题

C++11,CLion IDE

错误如下:

undefined reference to `aBag::aBag()'

main.cpp 很简单,目前还没有逻辑

#include <iostream>
#include "aBag.h"
using namespace std;

int main() {   

    aBag setA;

    return 0;    
}

以下是headeraBag.h,我无法编辑

#ifndef BAG_
#define BAG_

#include <vector>

typedef int ItemType;
class aBag
{
private:
    static const int DEFAULT_BAG_SIZE = 100;
    ItemType items[DEFAULT_BAG_SIZE]; // array of bag items
   int itemCount;                    // current count of bag items 
   int maxItems;                     // max capacity of the bag

   // Returns either the index of the element in the array items that
   // contains the given target or -1, if the array does not contain 
   // the target.
   int getIndexOf(const ItemType& target) const;   

public:
    aBag();
    int getCurrentSize() const;
    bool isEmpty() const;
    bool add(const ItemType& newEntry);
    bool remove(const ItemType& anEntry);
    void clear();
    bool contains(const ItemType& anEntry) const;
    int getFrequencyOf(const ItemType& anEntry) const;
};  // end Bag


#endif

aBag 的构造函数

#include "aBag.h"


aBag::aBag() : itemCount(0), maxItems(DEFAULT_BAG_SIZE)
{
} 

cmakefile.txt

cmake_minimum_required(VERSION 3.12)
project(project2)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp  aBag.cpp)

add_executable(project2 main.cpp)

make V=1 的输出

$make V=1
g++ -c -g -std=c++11  main.cpp
g++ -c -g -std=c++11  aBag.cpp
g++ -o project2 main.o aBag.o

它是某处的语法吗?我需要在某处添加 aBag.cpp 或 .h 作为源文件或目标吗?完全是别的东西?

发送帮助

这是你的 CmakeLists 文件,它不会将 aBag.cpp 添加到可执行源:

cmake_minimum_required(VERSION 3.12)
project(project2)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp  aBag.cpp)

# this is the correct way to use SOURCE_FILES list
add_executable(project2 ${SOURCE_FILES})