Your problem is called splicing.
In Vector, you declare an array of Objects by pointer:
When you
Now in addElement, you take an object passed by reference and put it into the array. The array holds only Objects, however. This means that the compiler is 'splicing' your parameter into two pieces -- the Object part and 'the 'rest' -- and it is copying ONLY THE Object PART into the array.
You have essentially lost the derived portion of your object at this point. This, of course, then makes your static_cast on line 9 in Mainc.cpp invalid.
This is not going to be an easy thing to fix. However, I will make one suggestion to you (kind of related):
Use dynamic_cast<> instead of static_cast<> for polymorphic 'downcasting'. This will actually cause the compiler to generate code to ensure the cast is legal. Had you done this on the above code, an exception would have been thrown on line 9.
(Note: if you dynamic_cast to a pointer, NULL is returned if the cast fails. if you dynamic_cast to a reference, an exception is thrown on failure instead).
In Vector, you declare an array of Objects by pointer:
Object* data;
.When you
new Object[100]
, you are allocating memory for 100 Objects. Object is an empty class.Now in addElement, you take an object passed by reference and put it into the array. The array holds only Objects, however. This means that the compiler is 'splicing' your parameter into two pieces -- the Object part and 'the 'rest' -- and it is copying ONLY THE Object PART into the array.
You have essentially lost the derived portion of your object at this point. This, of course, then makes your static_cast on line 9 in Mainc.cpp invalid.
This is not going to be an easy thing to fix. However, I will make one suggestion to you (kind of related):
Use dynamic_cast<> instead of static_cast<> for polymorphic 'downcasting'. This will actually cause the compiler to generate code to ensure the cast is legal. Had you done this on the above code, an exception would have been thrown on line 9.
(Note: if you dynamic_cast to a pointer, NULL is returned if the cast fails. if you dynamic_cast to a reference, an exception is thrown on failure instead).
Multiple Definition Of Main Error In Dev C Pdf
![Main Main](/uploads/1/2/6/0/126087281/656301867.jpg)
|
![C++ C++](/uploads/1/2/6/0/126087281/729611055.png)
Error Multiple Definition Of
Multiple definitions of 'main' suggests that you have another definition of main. Perhaps in another.c or.cpp file in your project. You can only have one function with the same name and signature (parameter types). Also, main is very special so you can only have one main function that can be used as the entry point (has either no parameters, one int, or an int and a char.) in your project. Nov 13, 2005 Linker error multiple definitions. E1071 svm auto tune parameters. C / C Forums on Bytes. Post your question and get tips & solutions from a community of 449,661 IT Pros & Developers. Your problem is called splicing. In Vector, you declare an array of Objects by pointer: Object. data. When you new Object100, you are allocating memory for 100 Objects. Object is an empty cl. This happens every single time i try to compile any project whether it would be a template given by Dev-C when creating a windows application or something I have been working on.I save all the files like the source, resource, header and all the files created by the compiler in the same directory, 'C:Dev-Cpp' from which I create a another. Jun 27, 2012 The linker will see that definition in multiple distinct object files, hence will complain about multiple definitions. I've had a quick look at all of your files, and you are making extensive usage of non-standard headers, which will prevent most people from recreating your problem even if they were willing to. # re: 解决 令人生厌的 multiple definition of 在其他文件中只要包含了global.h 就会独立的解释,然后生成每个文件生成独立的标示符。 在编译器连接时,就会将工程中所有的符号整合在一起,由于,文件中有重名变量,于是就出现了重复定义的错误。.