user1546022
user1546022

Reputation: 183

Why does my c++ code keep returning 'undefined reference to' one of my constructors when I try to compile?

I have been trying to get my code to compile for a while. Kept looking online for a solution to my problem but I didn't find one. Some help would be appreciated.

Here are some of the files that I have:

gate.h

#ifndef GATE_H
#define GATE_H

#include <vector>

class Gate {
private:
  std::vector<int> inWires, outWires;
  char function;
public:
  Gate (char function, const std::vector<int>& inputWires, const std::vector<int>& outputWires);
};

#endif

gate.cpp

#include "gate.h"

Gate::Gate (char functionality, const std::vector<int>& inputWires, const std::vector<int>& outputWires) {
  function = functionality;
  inWires = inputWires;
  outWires = outputWires;
}

circuit_file_reader.h

#ifndef CIRCUIT_FILE_READER_H
#define CIRCUIT_FILE_READER_H

#include <string>
#include <iostream>
#include <vector>
#include "circuit.h"
#include "gate.h"

Circuit readCircuit(std::string filename);

#endif

circuit_file_reader.cpp

#include "circuit_file_reader.h"

Circuit readCircuit (std::string filename) {
  std::vector<int> iw (1, 7);
  std::vector<int> ow (1, 8);
  Gate g0 ('a', iw, ow); // This is the problem
  std::vector<Gate> gates;
  // gates.push_back (g0);
  return Circuit (gates, 0);
}

test_circuit_file_reader.cpp

#include <iostream>
#include <string>
#include "circuit_file_reader.h"

int main(int argc, char** argv) {
  readCircuit("");
  std::cout << "Test Worked!" << std::endl;
  return 0;
}

Whenever I try to compile this code my compiler returns

circuit_file_reader.cpp:(.text+0xa5): undefined reference to `Gate::Gate(char, std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> > const&)'                                                       collect2: error: ld returned 1 exit status 

Which is strange because the Gate constructor has been defined so why can't it see it?

Upvotes: 1

Views: 310

Answers (2)

Harrison
Harrison

Reputation: 333

You didn't provide your compiler command, but I guess you didn't link gate.o while trying to generate the final binary.

Upvotes: 1

senex
senex

Reputation: 447

You declared the header in the header file, but its definition lies in the source (.cpp) file. You aren't specifying this to the compiler. You didn't specify what you're using to compile but if you compile all source files properly then it should work. Here is the example using g++.

You didn't provide the required circuit.h file to compile the program, so I made a dummy one just so it could compile.

Edit: I was asked to remove the picture so here is the g++ command in text.

g++ circuit_filer_reader.cpp gate.cpp main.cpp -o test
./test
Test Worked!

If for whatever reason anyone wants to compile it here is the dummy circuit.h file.

#include "gate.h"
struct Circuit
{
    Circuit(std::vector<Gate> gates, int test)
        {
            return;
        }
};

Upvotes: 1

Related Questions