1

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;
};
Michael Sync
  • 4,834
  • 10
  • 40
  • 58
  • It would be great if you guys can explain me why I can't do that in C++ as well.. Thanks. – Michael Sync Sep 08 '10 at 15:46
  • 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 (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 Sep 08 '10 at 16:02
  • 1
    You can't do x in C++ like you could in C# because C++ isn't C#! Get [a beginner book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read. – GManNickG Sep 08 '10 at 16:58

5 Answers5

5

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
  • 20,353
  • 1
  • 39
  • 56
3

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
  • 76,846
  • 14
  • 164
  • 167
  • Btw, tdammers said it best - STL already provides a lot of container classes so you don't even have to reinvent the wheel. – EboMike Sep 08 '10 at 16:59
1

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

mmonem
  • 2,811
  • 2
  • 28
  • 38
1

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
  • 54,333
  • 19
  • 131
  • 194
1

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
  • 134,909
  • 25
  • 265
  • 421