Error Redefinition Of
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up GCC compiler error: “redefinition…previously defined” up vote 7 down vote favorite 3 I'm getting a lot of " redefinition of x....x previously defined here". Please what does this error means? c++ gcc compiler-construction share|improve this question asked Apr 2 '09 at 1:00 caesar add a comment| 3 Answers 3 active oldest votes up vote 20 down vote You need to limit each file from being included only once. You can do this in 2 ways. 1) At the top of your header files put: #pragma once Or 2) if your compiler doesn't support that, put at the top/end of your header files: #ifndef _MYFILE_H_ #define _MYFILE_H_ ... #endif Replace MYFILE with the name of your file, and replace ... with the contents of the header file. share|improve this answer edited Apr 2 '09 at 7:11 unwind 254k38331460 answered Apr 2 '09 at 1:18 Brian R. Bondy 198k82474572 1 I think that #pragma only works in VC++, not in gcc. The #define trick is portable. –Gorpik Apr 2 '09 at 7:19 supoprted by gcc and vc++ –Brian R. Bondy Apr 2 '09 at 10:29 Wikipedia says #pragma once is supported by VC++ and gcc en.wikipedia.org/wiki/Pragma_once . –Max Lybbert Apr 3 '09 at 20:48 #pragma works fine with gcc.. I'm using it at this very moment after I reading about it and it solved a ton of errors! –mmoment Sep 7 '12 at 14:1
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up error: redefinition of class up vote 2 down vote favorite 2 Here's my code: // in main.cpp #include "iostream" #include "circle.cpp" #include "rectangle.cpp" #include "shape.cpp" using namespace std; http://stackoverflow.com/questions/707920/gcc-compiler-error-redefinition-previously-defined int main() { Shape shapes[10]; for (int i = 0; i < 10; i++){ if (i % 2) shapes[i] = Circle(5); else shapes[i] = Rectangle(10, 10); cout << shapes[i].getArea(); } return 0; } // in circle.cpp #include "shape.cpp" class Circle : public Shape { private: int radius; static const double PI = 3.14159265358979323846; public: Circle (int radius) : radius(radius) {} virtual int getArea() const { return PI * radius*radius; }; virtual int setRadius(int radius){ radius = radius; http://stackoverflow.com/questions/26179408/error-redefinition-of-class } }; // in rectangle.cpp #include "shape.cpp" class Rectangle : public Shape { private: int width; int height; public: Rectangle(int width, int height) : width(width), height(height){} virtual int getArea() const { return width * height; } virtual void setWidth(int width){ this->width = width; } virtual void setHeigth(int height){ this->height = height; } }; // in shape.cpp class Shape { public: virtual int getArea() const = 0; }; When compiling, I get this error: error: redefinition of 'class Shape' How can I fix this? c++ compiler-errors share|improve this question asked Oct 3 '14 at 12:59 Aviv Cohn 2,01031243 Check out "include guards" or #pragma once for compilers that support something like that. It looks like the header may be included several time. And the whole including cpp files... this is not normally how it is done. –Niall Oct 3 '14 at 13:03 add a comment| 2 Answers 2 active oldest votes up vote 7 down vote accepted You should structure your code between .h (headers) and .cpp files (implementation). You should include header files: .h Never include .cpp files. (Unless you know what you do, and that would be in really rare cases). Otherwise you're ending compiling several times your class, and you get the error your compiler is telling you: 'redefinition of class...' share|improve this answer edited Oct 3 '14 at 13:07 answered Oct 3 '14 a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack http://stackoverflow.com/questions/19965300/c-error-message-redefinition-of-functions Overflow the company Business Learn more about hiring developers or posting ads with us Stack http://forums.codeblocks.org/index.php?topic=20798.0 Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C++ Error message redefinition of functions up vote 2 down vote favorite I am using two stacks error redefinition to implement a queue class. My header file looks like: #ifndef _MyQueue_h #define _MyQueue_h using namespace std; template
"here I am" post here to differ human beings from SPAM bots. Home Help Search Login Register Wiki Code::Blocks » User forums » General (but related to Code::Blocks) » Error: redefinition of function « previous next » Send this topic Print Pages: [1] Go Down Author Topic: Error: redefinition of function (Read 1718 times) Phrosen Single posting newcomer Posts: 7 Error: redefinition of function « on: December 21, 2015, 06:14:55 pm » I have a problem that I think is related to my IDE. I am using Codeblocks (13.12).Here's my "set up":main.c (includes 'int main()')header.h (your typical header, includes prototypes)test.c (a random file, includes custom-made functions.)Here's the issue: All the functions works as intended, but when I compile my test.c I get an error (for each function) saying: "error: redefinition of ***"This issue doesn't affect anything, but it's annoying. I'm wondering if it's possible to get rid of it somehow? Maybe I'm doing something wrong when I'm creating my prototypes?Here's an example of what my functions and prototypes look like:Code: [Select]void func_showMenu(); //This is the prototype, in header.h
void func_showMenu(){
//This is the function, in test.c
}Is there some setting in Code::Blocks that can fix this issue? Logged headkase Almost regular Posts: 129 Re: Error: redefinition of function « Reply #1 on: December 21, 2015, 07:26:33 pm » Please post the complete contents of both your header and source files. If each function pair is the same then one complete pair will suffice. Make sure to include the boiler-plate code like the: define that is supposed to be in a header. « Last Edit: December 21, 2015, 07:28:07 pm by headkase » Logged stahta01 Lives here! Posts: 5393 Re: Error: redefinition of function « Reply #2 on: December 21, 2015, 09:28:05 pm » Post the complete header file; likely you did NOT guard it correctly.Edit: read this site rules. http://