tags:

views:

123

answers:

5
+1  Q: 

Nested type in C++

I have C# background and been working with C# for so many years.. Recently, I'm learning C++ and having some difficulties..

Basically, I'm trying to create the linked link class as below. I want to use my class as a data in struct node.

How can I fix this in C++? Thanks.

But it said that i can't use like that.

class Polynomial{
    public:
        Polynomial(pair<double, int>);  

        void add(Polynomial);
        Polynomial multiply(Polynomial);
        void print();
    private:
         struct node
         {
            Polynomial data;
            node *link;
         }*p;
};
+2  A: 

Couple of issues:

  • Polynomial doesn't have a default constructor, so the only way to create it is by using that custom constructor you have. However, your inner struct contains an object of type Polynomial. How is that supposed to be created? You can't embed objects that don't have a default constructor in classes unless you initialize them specifically in the container's constructor.

  • Your struct contains an object of the type of the parent class, which you're still in the process of defining! If anything, you need to make that struct its own class.

  • In general, you seem to do a lot by-value operations. This is very inefficient - you should always pass Polynomial by reference or pointer.

EboMike
Thanks a lot for explanation.. I appreciate it..
Michael Sync
Btw, tdammers said it best - STL already provides a lot of container classes so you don't even have to reinvent the wheel.
EboMike
+4  A: 

Your node struct contains a member variable of type Polynominal, but since node itself is declared inside Polynominal, the declaration of Polynominal isn't complete at that point.

I get the impression that you assume classes in C++ to work just like C#, but they don't. C++ isn't garbage-collected, and it doesn't automatically manage references for you when you use classes. A class in C++ behaves more like a struct in C#, and when you pass or declare it like in your example, it gets copied by value.

Another thing: C++ comes with STL, which contains a range of templates for all sorts of things, including a nice linked list (std::list).

tdammers
Oh.. std:list is linked list? didn't know that they are the same.. I think I will try using it.. Thanks a lot..
Michael Sync
Is "std:list" a STL Linked list?
Michael Sync
Yes, and it have a node-based implementation.
Klaim
Technically, `std::list` is usually implemented as a doubly-linked list, that is, each node has pointers to the next and previous nodes. This way, reverse iteration is as efficient as forward.
tdammers
+1  A: 

To fix it just use Polynomial &data; instead of Polynomial data; in the struct

mmonem
Thanks a lot....
Michael Sync
+1  A: 

Change that to Polynomial *data; and it will work just fine. And therein lies your clue as to what's wrong. Understanding that will bring great enlightenment.

One way of explaining it is that in C++ (unlike C#) a Polynomial and a float behave in exactly the same way with regards to how storage is allocated with them. In C# you can't do new float; (not to be confused with new Float();) and in C++ you can.

Omnifarious
Thanks a lot...
Michael Sync
+1  A: 

The points raised by EboMike are all valid, but just to make it compile (it's still unusable due to the constructability issue):

class Polynomial{
    public:
        Polynomial(pair<double, int>);  

        void add(Polynomial);
        Polynomial multiply(Polynomial);
        void print();
    private:
         struct node; // forward declaration creates incomplete type
         node *p; // OK to have pointer to incomplete type
};

struct Polynomial::node
{
    Polynomial data; // class Polynomial is complete now
    node *link;
};
Potatoswatter
Great.. Thanks a lot..
Michael Sync
Actually, that still won't compile since there is no default constructor for Polynomial.
EboMike
@Ebo: yeah, that's what I was referring to.
Potatoswatter