Newbie question

M

MN

Hi All, I'm new to C++. why isn't possible to use private variable
like in the next code. When compiling I get these errors:
error: ‘data’ was not declared in this scope
error: no pre-increment operator for type

#include <iostream>
using namespace std;

const int QUEUE_SIZE = 100;

class queue
{
private:
int count; // Number of data in queue
int data[QUEUE_SIZE]; // Data at position "count" in queue
public:
queue();
~queue();

void put(const int item); // Insert an item in the queue
void show(void); // Get the next item in the queue
};


queue::queue()
{
count = 0;
};
queue::~queue(){};

void put(const int item)
{
data[count] = item; //<--- Here a problem
++count; //<--- Here a problem
};

void show(void)
{
cout << "count is" << count<< "\n";
}
 
R

Rui Maciel

MN said:
void put(const int item)
{
data[count] = item; //<--- Here a problem
++count; //<--- Here a problem
};

void queue::put(const int item)


Rui Maciel
 
M

Maxim Yegorushkin

Hi All, I'm new to C++. why isn't possible to use private variable
like in the next code. When compiling I get these errors:
error: ‘data’ was not declared in this scope
error: no pre-increment operator for type

You forgot to use fully qualified function names for out-of-line
member function definitions. See below.
#include <iostream>
using namespace std;

const int QUEUE_SIZE = 100;

You may like to put this constant into queue class.
class queue
{
private:
        int count;                                              // Number of data in queue
        int data[QUEUE_SIZE];                           // Data at position "count" in queue
public:
        queue();
        ~queue();

        void put(const int item);                               // Insert an item in the queue
        void show(void);                                                    // Get the next item in the queue

No need to use (void) argument in C++. () is sufficient to say that
the function does accept arguments.
};

queue::queue()
{
        count = 0;};

queue::~queue(){};

No need to use ; after function definition.
void put(const int item)

It should be:

void queue::put(const int item)
{
        data[count] = item;     //<--- Here a problem
        ++count;                    //<--- Here a problem

};

void show(void)

Same here:

void queue::show(void)
 
B

Ben Kinslow

Your put implementation is not part of the class!

You need it like this...
void queue::put(const int item)


Hi All, I'm new to C++. why isn't possible to use private variable
like in the next code. When compiling I get these errors:
error: ‘data’ was not declared in this scope
error: no pre-increment operator for type

#include <iostream>
using namespace std;

const int QUEUE_SIZE = 100;

class queue
{
private:
int count; // Number of data in queue
int data[QUEUE_SIZE]; // Data at position "count" in queue
public:
queue();
~queue();

void put(const int item); // Insert an item in the queue
void show(void); // Get the next item in the queue
};


queue::queue()
{
count = 0;
};
queue::~queue(){};

void put(const int item)
{
data[count] = item; //<--- Here a problem
++count; //<--- Here a problem
};

void show(void)
{
cout << "count is" << count<< "\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top