错误:类计算机comp已经在computer.obj中定义 [英] Error: Class computer comp is already defined in computer.obj
问题描述
我目前正在玩C ++,并试图重建一个Tic Tac Toe批处理控制台游戏,我用C ++,但打了一堵墙,在那里我不知道如何摆脱的错误 TicTacToe.obj:error LNK2005:已经在computer.obj
中定义的class computer comp(?comp @@ 3Vcomputer @@ A)。我已经尝试从头中删除函数计算机的声明,并在C ++中的函数的定义,但没有修复错误。我唯一的方法,我想出了如何删除这个错误是删除对象名称,我不想这样做。我使用网站上提供的示例 http://www.cplusplus.com/doc/tutorial/ classes / 来设置类计算机。您可以提供任何错误,我目前有任何信息,或任何功能我可能不需要是最确定的欢迎,因为我想知道更多关于C ++。
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
// 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
computer.cpp
#include "stdafx.h"
#include "computer.h"
#include <iostream>
using namespace std;
computer::computer()
{
}
computer::~computer()
{
}
void computer::Select()
{
}
computer.h
computer.h
#pragma once
class computer
{
public:
computer();
~computer();
void Select(void);
} comp;
额外资讯:
Microsoft Visual Studio Professional 2013在运行Windows 7的笔记本电脑上。
I am using Microsoft Visual Studio Professional 2013 on a laptop running Windows 7.
推荐答案
正如您包含标题在两个模块中都有h
computer.cpp
和 TicTacToe.cpp
对象的相同定义 comp
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;
因此链接器会报错。
仅在一个cpp模块中定义对象。
Define the object only in one cpp module. The header should contain only the class definition.
例如
computer.h
computer.h
#pragma once
class computer
{
public:
computer();
~computer();
void Select(void);
};
TicTacToe.cpp
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);
}
这篇关于错误:类计算机comp已经在computer.obj中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!