-2

Just trying to write a basic c++ program for my college project

I declared a sample class called TestClass and put it in the testclass.h file in header files folder.

class TestClass
{
private:
    int an;
public:
    TestClass(void);
    int a[];
    void print();
    void compute();
    void get();
    ~TestClass(void);
};

And then to implement the above class, I created a new file in Source Files folders and then wrote this piece of code.

#include <iostream>
#include <conio.h>
#include "TestClass.h"



    TestClass:: vinayak(void)
    {

    }

    void TestClass:: print()
    {

    }
    void TestClass:: compute()
    {

    }
    void TestClass:: get()
    {

    }

    TestClass:: ~TestClass(void)
    {

    }

I then created a new file called Source.cpp and then wrote the piece of code.

#include "TestClass.h"
#include "TestClass.cpp"

void main()
{
    TestClass TObject = TestClass();
    TObject.get();

}

Here I am getting an error like below:

public: void __thiscall TestClass::compute(void)" already defined in Source.obj

Any ideas why ?

EDIT:

Based on @Marco's comment, I was able to compile the program. Also, I had to remove the constructor and the destructor declarations in the header file.

This made the program to work fine :)

  • `int a[];` is illegal in a class definition – M.M Jul 26 '14 at 23:28
  • It would be wise to call your variables something other than `TObject` if you are using C++Builder as it uses that name for a fundamental object. E.g . use something starting with a lower-case letter for a variable name. – M.M Jul 26 '14 at 23:30
  • @MattMcNabb: thank you :) I never knew.. – electronic_coder Jul 27 '14 at 07:04

4 Answers4

2

Every time you write

#include "something"

it's like copy-pasting the content of that "something" file into that specific location.

You're compiling a "TestClass" translation unit and a "Source" translation unit both containing the definition of your compute method.

Remove the cpp include in Source.cpp:

#include "TestClass.h"
// #include "TestClass.cpp" <- Remove this

void main()
{
    TestClass TObject = TestClass();
    TObject.get();

}

and the Source translation unit will know TestClass has a compute method, but the ODR (read it up) will be satisfied.


Summary of what you need to read about:

  • Difference between declaration / definition
  • What's a TU - translation unit
  • Difference between compiling and linking
  • What's the ODR and why it's important
  • What does the #include directive do
Marco A.
  • 43,032
  • 26
  • 132
  • 246
1

Header file must be protected against multiple inclusions.

Add #pragma once on top of your header file, or change it to:

#ifndef TEST_CLASS_INCLUDE
#define TEST_CLASS_INCLUDE
class TestClass
{
...
};
#endif

Also, you should never include .cpp files. Just include include TestClass.h from Source.cpp and make sure TestClass.cpp is compiled and TestClass.o is linked.

Jean

jpo38
  • 20,821
  • 10
  • 70
  • 151
0

You need to read up on linking

This line

#include "TestClass.cpp"

is not required

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

You are including the TestClass.cpp, you need to include only the header file

vsoftco
  • 55,410
  • 12
  • 139
  • 252