c++ - Why can't I call a class's non-default constructor in another file? -
this question has answer here:
- “undefined reference to” in g++ cpp 2 answers
i new c++. have started writing class called row, , trying call non-default constructor create row object in separate main.cpp file, keep getting error not understand. can explain me i've done wrong?
here 3 files:
row.h
#ifndef row_h #define row_h #include<vector> #include<iostream> class row { std::vector<int> row; public: // constructor row(std::vector<int> row); }; #endif row.cpp
#include<vector> #include<iostream> #include "row.h" // constructor row::row(std::vector<int> row_arg) { row = row_arg; } main.cpp
#include<vector> #include<iostream> #include "row.h" using namespace std; int main() { vector<int> v = {1, 2, 3, 4}; row row(v); return 0; } the error receive upon trying compile main.cpp this:
/tmp/ccjvgzew.o:pascal_classes.cpp:(.text+0x71): undefined reference `row::row(std::vector<int, std::allocator<int> >)' /tmp/ccjvgzew.o:pascal_classes.cpp:(.text+0x71): relocation truncated fit: r_x86_64_pc32 against undefined symbol `row::row(std::vector<int, std::allocator<int> >)' collect2: error: ld returned 1 exit status
that looks linker error, not compiler error, , guess you're getting error because either
- you forgot compile
row.cpp, or - you forgot link
row.ofinal executable.
if you're compiling command-line, make sure compile both main.cpp , row.cpp. should fix things!
Comments
Post a Comment