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



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

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

以下是标题 aBag.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)

使 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})

相关内容

最新更新