Thread: can anyone find whats my misttake?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    can anyone find whats my misttake?

    hi i just done some coding about base class shape and derived class twod and threed..
    can anyone show me my wrong?
    Code:
    class Shape
    {
    public:
    Shape(){};
    void print();
    }
    
    class TwoD:public Shape
    {
    
    public:
    TwoD( double a =0.0,double b = 0.0): length(a),width(b){
    setdata(a,b);
    }
    
    virtual double area()
    {return 0}
    
    void setdata(double a,double b){
    length = a;
    width = b;
    }
    
    void print(){
    cout<<length<<','<<width<<endl;
    }
    protected:
    double length,width;
    }
    
    class Threed:public TwoD{
    friend ostream &operator<<(ostream &stream,Threed o);
    
    friend Threed operator*(int &Number,Threed &o);
    public:
    Threed operator++();
    Threed operator*(int &Number);
    Threed(double a,double b,double c):TwoD(a,b),height(c){
    setdata(a,b,c);}
    void print()
    {
    cout<<length<<','<<width<<','<<height<<endl;
    }
    void setdata( double a, double b,double c)
    {
    length = a;
    width = b;
    height = c;
    }
    
    
    protected:
    double height;
    }
    Threed Threed::operator++(){
    length++;width++;height++;return *this;
    }
    Threed operator*(int &number,Threed &o)
    { 
    Threed a;
    a.length= number*o.length;
    a.width= number*o.width;
    a.height= number*o.height;
    return a;
    ostream &operator<<(ostream &stream, Threed o)
    {
      stream << o.length << " ";
      stream << "(" << o.width << ") ";
      stream << o.height << "-";
    
      return stream; // must return stream
    }
    
    int main()
    {
    ThreeD a(1,2,3),b(1,2),c(1);
    ++a;
    c = a*2;//use member function overloading
    c= 2*a;//use global function overloading
    return 0;
    }
    Last edited by MyRedz; 03-21-2009 at 06:13 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    At first glance you are missing a couple of semicolons, perhaps some brackets and braces, need to decide whether it is ThreeD or Threed, etc. Some of which would be more obvious if you indented your code properly.

    You should know that class declarations end with ; (after the closing brace). From there on you just have to read the compiler errors and fix them.

    Also, you wouldn't be overwhelmed with errors, if you compiled and tested your code more often.

    Conceptually I'm not so sure whether a ThreeD shape is a kind of TwoD shape. Perhaps both might be inherited from Shape, and ThreeD can use an instance of TwoD as a member for width and length dimension.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    ok..from my meaning and questions it says.
    shape is base class.
    class twod inherits public from class shape
    and class threed inherits public from class twod
    in twod width and length are declared,
    and in threed another height is declared;
    so here's my revised coding
    Code:
    #include <iostream>
    using namespace std;
    class Shape
    {
    public:
    Shape(){};
    void print();
    };
    
    class TwoD:public Shape
    {
    
    public:
    TwoD( int a =0,int b = 0): length(a),width(b){
    setdata(a,b);
    }
    
    
    void setdata(int a,int b){
    length = a;
    width = b;
    }
    
    void print(){
    cout<<length<<','<<width<<endl;
    }
    protected:
    int length,width;
    };
    
    class Threed:public TwoD
    {
    friend ostream &operator<<(ostream &stream,Threed o);
    
    friend Threed operator*(int Number,Threed o);
    public:
    Threed operator++();
    Threed operator*(int Number);
    Threed(int a =0,int b =0,int c =0):TwoD(a,b),height(c){
    
    setdata(a,b,c);
    }
    void print()
    {
    cout<<length<<','<<width<<','<<height<<endl;
    }
    void setdata( int a, int b,int c)
    {
    length = a;
    width = b;
    height = c;
    }
    
    
    protected:
    int  height;
    };
    Threed Threed::operator++(){
    ++length;
    ++width;
    ++height;
    return *this;
    }
    Threed operator*(int number,Threed o)
    { 
    Threed a;
    a.length= number*o.length;
    a.width= number*o.width;
    a.height= number*o.height;
    return a;
    }
    Threed Threed::operator*(int number)
    {
    Threed a,b;
    
    a.length=b.length*number;
    a.width=b.width*number;
    a.height=b.height*number;
    return a,b;
    }
    
    ostream &operator<<(ostream &stream, Threed o)
    {
      stream << o.length <<endl;
      stream << o.width << endl;
      stream << o.height <<endl;
    
      return stream; // must return stream
    }
    
    int main()
    {
    Threed a(1,2,3),b(1,2),c(1);
    ++a;//prefix overloading
    c = a*2;//use member function overloading
    c= 2*a;//use global function overloading
    return 0;
    
    }
    error is
    :\Users\Guest\aa.cpp(84) : error C2248: 'length' : cannot access protected member declared in class 'TwoD'
    C:\Users\Guest\aa.cpp(28) : see declaration of 'length'
    C:\Users\Guest\aa.cpp(85) : error C2248: 'width' : cannot access protected member declared in class 'TwoD'
    C:\Users\Guest\aa.cpp(28) : see declaration of 'width'
    C:\Users\Guest\aa.cpp(86) : error C2248: 'height' : cannot access protected member declared in class 'Threed'
    C:\Users\Guest\aa.cpp(56) : see declaration of 'height'
    Error executing cl.exe.

    aa.obj - 3 error(s), 0 warning(s)

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, I can't reproduce this error with any compiler. Try rebuilding. The only thing wrong is:

    Code:
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 72: warning: expression has no effect
          return a,b;
                 ^
    And this is what your code would look like with indentation:

    Code:
    #include <iostream>
    using namespace std;
    class Shape {
    public:
        Shape() {};
        void print();
    };
    
    class TwoD:public Shape {
    
    public:
        TwoD( int a =0,int b = 0): length(a),width(b) {
            setdata(a,b);
        }
    
    
        void setdata(int a,int b) {
            length = a;
            width = b;
        }
    
        void print() {
            cout<<length<<','<<width<<endl;
        }
    protected:
        int length,width;
    };
    
    class Threed:public TwoD {
        friend ostream &operator<<(ostream &stream,Threed o);
    
        friend Threed operator*(int Number,Threed o);
    public:
        Threed operator++();
        Threed operator*(int Number);
        Threed(int a =0,int b =0,int c =0):TwoD(a,b),height(c) {
    
            setdata(a,b,c);
        }
        void print() {
            cout<<length<<','<<width<<','<<height<<endl;
        }
        void setdata( int a, int b,int c) {
            length = a;
            width = b;
            height = c;
        }
    
    
    protected:
        int  height;
    };
    Threed Threed::operator++() {
        ++length;
        ++width;
        ++height;
        return *this;
    }
    Threed operator*(int number,Threed o) {
        Threed a;
        a.length= number*o.length;
        a.width= number*o.width;
        a.height= number*o.height;
        return a;
    }
    Threed Threed::operator*(int number) {
        Threed a,b;
    
        a.length=b.length*number;
        a.width=b.width*number;
        a.height=b.height*number;
        return a,b;
    }
    
    ostream &operator<<(ostream &stream, Threed o) {
        stream << o.length <<endl;
        stream << o.width << endl;
        stream << o.height <<endl;
    
        return stream; // must return stream
    }
    
    int main() {
        Threed a(1,2,3),b(1,2),c(1);
        ++a;//prefix overloading
        c = a*2;//use member function overloading
        c= 2*a;//use global function overloading
        return 0;
    
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    ok...the code problem is i use protected for the three values..
    and i already made it friend for class twod and threed..
    so why it stilll cannot access from overloading ostream?
    i dun want to delete protected keyword if i can still use it in my coding..
    any ideas?
    Code:
    #include <iostream>
    using namespace std;
    class Shape {
    public:
        Shape() {};
        void print();
    };
    
    class TwoD:public Shape {
      friend ostream &operator<<(ostream &stream,TwoD &);
    public:
        TwoD( int a =0,int b = 0): length(a),width(b) {
            setdata(a,b);
        }
    
    
        void setdata(int a,int b) {
            length = a;
            width = b;
        }
    
        void print() {
            cout<<length<<','<<width<<endl;
        }
    protected:
        int length,width;
    };
    
    class Threed:public TwoD {
        friend ostream &operator<<(ostream &stream,Threed o);
    
        friend Threed operator*(int Number,Threed o);
    public:
        Threed operator++();
        Threed operator*(int Number);
        Threed(int a =0,int b =0,int c =0):TwoD(a,b),height(c) {
    
            setdata(a,b,c);
        }
        void print() {
            cout<<length<<','<<width<<','<<height<<endl;
        }
        void setdata( int a, int b,int c) {
            length = a;
            width = b;
            height = c;
        }
    
    
    protected:
        int  height;
    };
    Threed Threed::operator++() {
        ++length;
        ++width;
        ++height;
        return *this;
    }
    Threed operator*(int number,Threed o) {
        Threed a;
        a.length= number*o.length;
        a.width= number*o.width;
        a.height= number*o.height;
        return a;
    }
    Threed Threed::operator*(int number) {
        Threed a;
    
        a.length=length*number;
        a.width=width*number;
        a.height=height*number;
        return a;
    }
    
    ostream &operator<<(ostream &stream, Threed o) {
        stream << o.length <<endl;
        stream << o.width << endl;
        stream << o.height <<endl;
    
        return stream; // must return stream
    }
    
    int main() {
        Threed a(1,2,3),b(1,2),c(1);
        ++a;//prefix overloading
        c = a*2;//use member function overloading
        c= 2*a;//use global function overloading
    	c.print();
        return 0;
    
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think you've quite got the hang of what we're saying here. The code you've posted compiles beautifully without any problems about not being able to access things, and prints "4,6,8" when it runs. If you're having problems with some other code, then you'll have to provide details about that code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM