943,732 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 458
  • C++ RSS
Nov 23rd, 2008
0

Strange inheritance bug with type conversion?

Expand Post »
Consider this working code:
#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
2

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..

#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.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Nov 23rd, 2008
0

Re: Strange inheritance bug with type conversion?

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)
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: bidirectional map
Next Thread in C++ Forum Timeline: Matrix Multiplication





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC