0

I'm sure this is a common question, and I've been looking through similar questions; but i'm unable to get this resolved

C++11, CLion IDE

Error as follows:

undefined reference to `aBag::aBag()'

main.cpp is simple and has no logic as of yet

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

int main() {   

    aBag setA;

    return 0;    
}

the following is header aBag.h, which i'm unable to edit

#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

constructor for 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)

output for 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

is it syntax somewhere? do i need to be adding aBag.cpp or .h as a source file or target somewhere? something else entirely?

send help

sidd
  • 115
  • 1
  • 1
  • 6
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – 1201ProgramAlarm Sep 08 '18 at 03:47
  • 2
    Does Clion output its compilation line? if it does, can you post it? are you sure that aBag.cpp gets compiled? put a deliberate error in it and make sure it does ... – OrenIshShalom Sep 08 '18 at 03:54
  • 1
    The setup of your code looks right. The problem is with your `CmakeLists.txt`. Can you post that here as well. – smac89 Sep 08 '18 at 03:57
  • 1
    Show the output of `cmake V=1` or `make V=1`. – jww Sep 08 '18 at 03:59
  • Compilation line points to `aBag setA; ` , adding a random error to aBag.h breaks upon compilation, so i dont believe it to be an error in the header. – sidd Sep 08 '18 at 04:12
  • Edited post with Cmakelists.txt – sidd Sep 08 '18 at 04:15
  • editted post with make V=1 output – sidd Sep 08 '18 at 04:32

1 Answers1

3

It's your CmakeLists file, it doesn't add aBag.cpp to the executable sources:

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})
amin
  • 3,672
  • 5
  • 33
  • 61