1

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?

linuxuser27
  • 7,183
  • 1
  • 26
  • 22
user1546022
  • 183
  • 2
  • 11
  • 2
    Very likely in http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris Mar 25 '17 at 19:00
  • How do you compile it? What is your exact command(s)? – Jonas Mar 25 '17 at 19:05
  • I see [no problems](http://cpp.sh/5qpby). – Dmitry Sokolov Mar 25 '17 at 19:08
  • Maybe you need to "clean" your stuff and recompile everything. That gets rid of unfrequent weird errors for me.... – user Mar 25 '17 at 19:08
  • 1
    Do you know the difference between compiler errors and linker errors? This is the latter. Do you link with gate.o? As an aside, it is better to do initialization of the members in Gate::Gate: you are assigning them after they have been default constructed. – drRobertz Mar 25 '17 at 19:12

2 Answers2

1

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;
        }
};
senex
  • 447
  • 3
  • 14
1

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

Harrison
  • 313
  • 3
  • 15