85406

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

Question:

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 <a href="http://www.cplusplus.com/doc/tutorial/classes/" rel="nofollow">http://www.cplusplus.com/doc/tutorial/classes/</a> 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.

Answer1:

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); }

Answer2:

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.

Answer3:

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.

Recommend

  • Exception in cvConvertScale in OpenCV calling solvePnP
  • In a MEAN stack, how can I do one-time MongoDB indexing?
  • Python PIL remove sections of an image based on its colour
  • RPMs for IDAS IoTAgents GE, from Fiware
  • Consuming a web service with the Netbeans Platform
  • c++ search a vector for element first seen position
  • XNU incudes in Kext
  • c++ regex_replace not doing intended substitution
  • NHibernate manually control fetching
  • Salesforce Different WSDL files and when to use
  • How to disable all widgets inside Panel or inside Composite?
  • SharedPreferences or SQLite Database?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • How to attach a node.js readable stream to a Sendgrid email?
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Yii2: Config params vs. const/define
  • Typescript - Unable to get 'import' statement to function
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Is there any way to access browser form field suggestions from JavaScript?
  • Optimizing database types to compact database (SQLite)
  • swift auto completion not working in Xcode6-Beta
  • Bug in WPF DataGrid
  • TFS: Get latest causes slow project reloading
  • Cross-Platform Protobuf Serialization
  • Javascript Callbacks with Object constructor
  • JSON with duplicate key names losing information when parsed
  • How to make Safari send if-modified-since header?
  • Build own AppleScript numerical error handling
  • Do I've to free mysql result after storing it?
  • Jquery - Jquery Wysiwyg return html as a string
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • KeystoneJS: Relationships in Admin UI not updating
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • How to Embed XSL into XML