1

I am fairly new to C++. I have started writing a class called Row, and I am trying to call a non-default constructor to create a row object in a separate main.cpp file, but I keep getting an error that I do not understand. Can anyone explain to me what I've done wrong?

Here are my three 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 I receive upon trying to compile main.cpp is this:

/tmp/ccJvGzEW.o:pascal_classes.cpp:(.text+0x71): undefined reference to `Row::Row(std::vector<int, std::allocator<int> >)'
/tmp/ccJvGzEW.o:pascal_classes.cpp:(.text+0x71): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Row::Row(std::vector<int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status
CJH
  • 13
  • 2

1 Answers1

1

That looks like a linker error, not a compiler error, and my guess is that you're getting this error because either

  1. you forgot to compile Row.cpp, or
  2. you forgot to link Row.o into the final executable.

If you're compiling from the command-line, make sure to compile both main.cpp and Row.cpp. That should fix things!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • That totally did it. I didn't realize I had to compile both .cpp files. Thank you! – CJH Jan 16 '16 at 01:20