Error: Class computer comp is already defined in computer.obj

237 Views Asked by At

I am currently playing with C++, and attempting to rebuild a Tic Tac Toe batch console game I made in C++, but have hit a wall, where I cannot figure out how to get rid of the error TicTacToe.obj : error LNK2005: "class computer comp" (?comp@@3Vcomputer@@A) already defined in computer.obj. I have tried removing the declaration of the function computer from the header, and the definition of the function in the C++, but that didn't fix the error. The only way I figured out how to remove this error was to remove the object name, which I kind of don't want to do. I used the example given on the website http://www.cplusplus.com/doc/tutorial/classes/ to set up the class computer. Any information you can provide on any errors that I currently have, or any functions I may not need are most definately welcome, as I am wanting to know much much more about C++.

CODE:

TicTacToe.cpp

// TicTacToe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include "computer.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    comp.Select();
    Sleep(1000);
}

computer.cpp

#include "stdafx.h"
#include "computer.h"
#include <iostream>
using namespace std;

computer::computer()
{
}

computer::~computer()
{
}

void computer::Select()
{
}

computer.h

#pragma once
class computer
{
public:
    computer();
    ~computer();
    void Select(void);
} comp;

EXTRA INFO:

I am using Microsoft Visual Studio Professional 2013 on a laptop running Windows 7.

3

There are 3 best solutions below

0
On BEST ANSWER

As you included header "computer.h" in both modules computer.cpp and TicTacToe.cpp then the both modules contain the same definition of object comp

pragma once
class computer
{
public:
    computer();
    ~computer();
    void Select(void);
} comp;

So the linker issues the error.

Define the object only in one cpp module. The header should contain only the class definition.

For example

computer.h

#pragma once
class computer
{
public:
    computer();
    ~computer();
    void Select(void);
};

TicTacToe.cpp

// TicTacToe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include "computer.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    computer comp;

    comp.Select();
    Sleep(1000);
}
0
On

You are defining comp in the header, and so in every .cpp that includes that header, so you are breaking the One Definition Rule.

Instead you can declare it in the header:

extern computer comp;

And then define it in exactly one .cpp:

computer comp;

Which will still allow you to access it from any .cpp that includes the header.

0
On

You have to remove comp from the header file. Create the object in a cpp file like this:

computer comp;

You said you don't want to do that. If that causes some other problem for you then post a new question about that problem.