Pratik Singhal
Pratik Singhal

Reputation: 6492

Trouble making a header file

I am trying to make my own header file which will contain a class Vector.
To create a header file, as advised by some websites, I have created two different files :-

1) collections.h (contains declarations) 2) collections.cpp (contains definition)

Another file is main.cpp, which contains the main function, and in which I will be using collections.h

All these files are in the same directory

The trouble I am having is that compiler is showing the error

Undefined reference to Vector::Vector(int, int)
and so on for all the functions in my class.

I have made sure that there is a #include "collections.h" line in both collections.cpp as well as main.cpp

How can I solve the above problem?

I am using gcc compiler under ubuntu 12.04

Upvotes: 0

Views: 49

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409452

You need to build both source files and link them together.

On a Linux command line, you can do it simplest by providing both source files to gcc:

$ g++ -Wall main.cpp collections.cpp -o my_program

Note: I added the -Wall option to enable more warnings by default. It's always a very good habit to enable more warnings, as they can often point out logical errors or possible places where you have undefined behavior.

Upvotes: 1

Beta
Beta

Reputation: 99172

First build the object files:

gcc -c main.cpp -o main.o
gcc -c collections.cpp -o collections.o

then link them together:

gcc collections.o main.o -o main

Upvotes: 2

Related Questions