0

I am trying to create a class with a vector with undefined size as class member. My code:

main.cpp

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

int main() {
    Polynom p {{7,-2,3,0,0,8}};
    return 0;
}

Polynom.h

#ifndef INC_0001_POLYNOM_H
#define INC_0001_POLYNOM_H

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

class Polynom {
private:
    vector<int> vec;
public:
    Polynom(vector<int> vec);
};


#endif //INC_0001_POLYNOM_H

Polynom.cpp

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

Polynom::Polynom(vector<int> vec){
    this->vec=vec;
}

But whenever i try to run this code i get following error,

main.cpp:7: undefined reference to `Polynom::Polynom(std::vector<int, std::allocator<int> >)'

The code was longer initially but I had way too many errors when i tried to run it. So I tried to run the bare bone...

PS:I am trying to create a class where i have to input for example 7,-2,3,0,0,8 then couts 8x^5+0x^4+0x^3+3x^2+-2x^1+7x^0 (with a method), but it is undefined how many numbers i type in.

Edit: I use CLion and the CMakeList is:

cmake_minimum_required(VERSION 3.7)
project(0001)

set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES main.cpp)
add_executable(0001 ${SOURCE_FILES})

MinGW version 5.0
Bundled CMake 3.7.2
Bundled GDB 7.11.1

What is an undefined reference/unresolved external symbol error and how do I fix it?

Isnt helpful because I am relative new to C++ and on that thread it is writen so complicated that I dont understand anything (and dont even how it related to my problem beside the error message)

And there is another Problem according to the CLion there is a parameter type mismatch (At Polynom p {{7,-2,3,0,0,8}};). Which means that there should be other problems.

How do i adjust my constructor so it can build a class this way Polynom p {{7,-2,3,0,0,8}};

sapi3ntia
  • 15
  • 4
  • The problem is with how you are building the code. What exact build steps are you running? You're somehow not linking in `Polynom.cpp`. – John Zwinck Jul 02 '17 at 02:55
  • How do I link in Polynom.cpp? I am trying to create a class where i have to input for example 7,-2,3,0,0,8 then couts 8x^5+0x^4+0x^3+3x^2+-2x^1+7x^0 – sapi3ntia Jul 02 '17 at 03:03
  • 2
    Polynom.h need not #include "Polynom.h" – 2785528 Jul 02 '17 at 03:04
  • Originally it didnt have #include "Polynom.h"... but when i was trying to solve the problem I tried everything possible (even though many things I did didnt have practical uses) – sapi3ntia Jul 02 '17 at 03:08
  • I think the code is 'good-enough' to compile ... my version does, but I take short-cuts, did all in one file. Perhaps you could show us your compile / build command? – 2785528 Jul 02 '17 at 03:17
  • Although it is stated below by li.hao `Polynom p {{7,-2,3,0,0,8}};` is a C++11 feature. So you need to make sure your compiler supports C++11 – macroland Jul 02 '17 at 03:38
  • I use CLion (for students) (mingw32) (I dont use any commands i just click on run button on the top right corner) – sapi3ntia Jul 02 '17 at 03:44
  • What exact build steps are you running? What is your build configuration? What compilation commands are you running? Without this information no solution can be found. – John Zwinck Jul 02 '17 at 03:46
  • i use cmake_minimum_required(VERSION 3.7) project(0001) set(CMAKE_CXX_STANDARD 14) set(SOURCE_FILES main.cpp) add_executable(0001 ${SOURCE_FILES}) Which mean i use C++14 – sapi3ntia Jul 02 '17 at 03:47
  • I use MinGW version 5.0 Bundled CMake 3.7.2 Bundled GDB 7.11.1 – sapi3ntia Jul 02 '17 at 03:53

1 Answers1

0

You should add the code below to the link options:

g++ main.cpp polynom.cpp -std=c++0x

I tried this method to solve your problem.

li.hao
  • 16
  • 1
  • OK, in CLion u have to add the file into the CMakeLists if it didnt added it when it create the file for example: set(SOURCE_FILES main.cpp Smoothie.cpp Smoothie.h Zutat.cpp Zutat.h) add_executable(CLion ${SOURCE_FILES} Smoothie.cpp Smoothie.h Zutat.cpp Zutat.h) – sapi3ntia Jul 02 '17 at 22:33