| | |
Strange inheritance bug with type conversion?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Consider this working code:
Can you explain why I'm getting this compile error when attempting to do some inheritance? I want it to call the Base::print (int) function which in turn should call the NewOne::print( string) function..
Note I've also tried making the remaining functions virtual but to no avail.
#include <iostream>
using namespace std;
class Base
{
public:
void print(string val)
{
cout<<val<<endl;
}
virtual void print(int val)
{
char temp[1000];
sprintf( temp, "%d", val );
string tempstring = temp;
print ( tempstring );
}
};
int main()
{
Base a;
string whatever= "BLAH";
a.print(whatever);
a.print(2);
}
./Test3
BLAH
2Can you explain why I'm getting this compile error when attempting to do some inheritance? I want it to call the Base::print (int) function which in turn should call the NewOne::print( string) function..
#include <iostream>
using namespace std;
class Base
{
public:
virtual void print(string val)=0;
void print(int val)
{
char temp[1000];
sprintf( temp, "%d", val );
string tempstring = temp;
print ( tempstring );
}
};
class NewOne : public Base
{
public:
virtual void print(string val)
{
cout<<"In NewOne: "<<val<<endl;
}
};
int main()
{
NewOne a;
string whatever= "BLAH";
a.print(whatever);
a.print(2);
}
g++ Test2.cpp -o Test2
Test2.cpp: In function `int main()':
Test2.cpp:32: error: invalid conversion from `int' to `const char*'
Test2.cpp:32: error: initializing argument 1 of `std::basic_string<_CharT,
_Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT =
char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'Note I've also tried making the remaining functions virtual but to no avail.
Last edited by winbatch; Nov 23rd, 2008 at 3:12 pm.
Looks like this was the solution:
http://www.parashift.com/c++-faq-lit....html#faq-23.9
(I added a 'using Base::print' to my NewOne class)
http://www.parashift.com/c++-faq-lit....html#faq-23.9
(I added a 'using Base::print' to my NewOne class)
![]() |
Other Threads in the C++ Forum
- Previous Thread: bidirectional map
- Next Thread: Matrix Multiplication
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





