我确定这是一个常见问题,并且我一直在研究类似的问题;但我无法解决这个问题
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)
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添加为源文件或目标位置?完全是其他东西吗?
发送帮助
答案 0 :(得分:2)
这是您的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})