JohnOne Posted February 22, 2013 Share Posted February 22, 2013 (edited) I have this error, and I cannot understand why it is happening. I'm using VS 2010, win32 project. The error says that the method Full is already defined in MyClass object. Well yes, I know it is, it is a prototype of the implementation which is defined outside of the class. If I comment out the implementation code the project compiles fine without issue. Both class and method are in the same header file. There are no other functions, methods, or vars with the same name of either class or method. I've been trying to figure it out too long, and need some help if anyone can offer it. class MyClass { int cx, cy; HDC hdc, hDest; public: MyClass() : cx(0), cy(0) { hdc = GetDC(NULL); hDest = CreateCompatibleDC(hdc); } MyClass(int vx, int vy) { hdc = GetDC(NULL); hDest = CreateCompatibleDC(hdc); cx = vx; cy = vy; } void Full(int, int, int, int, std::string); void Region(int, int, int, int, std::string); }; void MyClass::Full(int x, int y, int w, int h, std::string path){ return; } Edited February 22, 2013 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
JohnOne Posted February 22, 2013 Author Share Posted February 22, 2013 It does not like the implementation code in the same file as the class, the problem goes away when it is moved into .cpp file. Perhaps it's just me, but that seems a bit daft. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
wraithdu Posted February 22, 2013 Share Posted February 22, 2013 Everything would probably work ok in the cpp file, definition and implementation. I guess the compiler doesn't like implementations in header files, not exactly that they are in the *same* file. JohnOne 1 Link to comment Share on other sites More sharing options...
JohnOne Posted February 22, 2013 Author Share Posted February 22, 2013 Really weird, in most tutorials I have read, it says best practice is that a header file should be able to compile and work on it's own, so this is really strange. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
JohnOne Posted February 22, 2013 Author Share Posted February 22, 2013 It has just occurred to me that this project is the first I have made where I specified no precompiled headers. Setting to use precompiled headers fixes my problem. Cheers for looking folks. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
JohnOne Posted February 22, 2013 Author Share Posted February 22, 2013 This is cracking me up now. As I said above setting properties of project to use precompiled headers worked (seemingly) But after I added to the class in the same manner, the new method showed the same error, so I'm back to square one, being confused and frustrated. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Solution Ascend4nt Posted February 22, 2013 Solution Share Posted February 22, 2013 John, Because you declared that function outside of the class, for every C++ file that includes the header, duplicate versions of that function will be created - which is correct compiler behavior. That's why you should either declare the function in its own C++ file, declare it as inline, or put it inside the class object definition (which behaves like inline). JohnOne 1 My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
JohnOne Posted February 22, 2013 Author Share Posted February 22, 2013 Cheers, I've just capitulated and put the functions in their own cpp file. For the record there is only one file that is included in all cpp files and that is stdafx.h, which has header guards and contains the other headers which the project is dependent on. You know when you just know something is not quite right though? Thanks for the explanation, glad to put this hiccup behind me. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Richard Robertson Posted February 23, 2013 Share Posted February 23, 2013 You need to make the distinction between declaring a name and defining it. Declarations can occur repeatedly (forward declarations as well as extern (meaning another file)) without problems, but definitions should only occur once. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now