Converting struct to class? xxx is private within this context

Not sure how to alleviate this issue (see title). I went ahead and was in the process (not finished I assume due to the error) of revamping a program that used struct: https://pastebin.com/FDBjNeUn --(original code before revamping to class)

Currently I have these for my main.cpp, Product.h, and Product.cpp:

https://pastebin.com/eAwpLGMM --main.cpp

https://pastebin.com/TLYasaq9 --Product.h

https://pastebin.com/QbYUmFQy --Product.cpp
This:
1
2
3
4
class Product{
    Product();
//...
};
is equivalent to this:
1
2
3
4
5
struct Product{
private:
    Product();
//...
};
In other words, you're making the default constructor of Product() private, yet you're using it in main(). Make it public.
Ok, that seems to get rid of almost all the errors, however on line 25:
products[i] = new Product;

it is giving me the following error:
'Product::Product()' is private within this context

So I make it a comment, and no errors so, I run it and get infinite output for the following sample input:

4011
10
3115
2.5
0
n
4129
3.6
4139
4.3
0
c

I was instructed to make it private, so I think there is some sort of way to access it regardless of it being private or not via main.
If the 0-arg Product constructor must be private, then don't call it.
Call new Product(code, string, price);.

If you're getting some infinite loop, my first guess would be you entered invalid data and your input stream is now in a bad state.
In the loop that is infinite, add:
1
2
3
4
if (!cin)
{
    cout << "Error: Bad input state\n";
}

This will at least confirm that (but won't stop the loop).

To clear the state of cin, you can do:
1
2
3
4
5
6
7
8
9
10
11
#include <limits>

// ...

if (!cin)
{
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

// attempt to get user input again 
Last edited on
Topic archived. No new replies allowed.