Reputation: 23
I have to make a linked list. I'm not sure if I'm doing this right first of all. But basically the project says I'm not allowed to make a .cpp file but I have to create 4 inline statements through the .h file.
class Queue{
private:
struct QueueNode{
int size;
std::string party;
QueueNode *next;
};
QueueNode *front;
QueueNode *rear;
public:
// Constructor
Queue();
// Destructor
~Queue();
// Queue operations
std::string getPartyName() const { return party; } // This and next 3 lines give error with the private variables
int getSize() const { return size; }
void setPartyName(std::string p) const{ party = p; }
void setSize(int s) const{ size = s; }
};
Upvotes: 0
Views: 159
Reputation: 2670
You are halfway there. You need to declare the functions in the header file, as you have done.
The missing step is that you need to not define the functions within the class body but instead define them below the class declaration using the class name with the scope resolution operator and the inline operator.
The other piece you are missing is that you are declaring functions in your Queue class that are likely meant for your QueueNode struct.
class Queue{
private:
struct QueueNode{
int size;
std::string party;
QueueNode *next;
};
QueueNode *front;
QueueNode *rear;
public:
// Constructor
Queue();
// Destructor
~Queue();
// Queue operations
std::string getPartyName( const QueueNode * const node ) const;
int getSize( const QueueNode * const node ) const;
void setPartyName( QueueNode & out_node, std::string & p);
void setSize( QueueNode & out_node, int s);
};
inline std::string Queue::getPartyName( const QueueNode * const node ) const
{
return node->party;
}
inline int getSize( const QueueNode * const node ) const {
return node->size;
}
inline void Queue::setPartyName( QueueNode & out_node, std::string & p) {
out_node.party = p;
}
inline void Queue::setSize( QueueNode & out_node, int s) {
out_node.size = s;
}
It isn't clear what nodes you intend to set these values for, so I'll fill in the functions with some assumptions.
Either way I think you get the point. If you want to define inline functions within a header class, you cannot define the body within the class itself. You must declare it inline, which is a compiler suggestion, outside the body of the class. I know I didn't complete define the functionality of a linked list, I will leave that to you. But, this should answer the inline question.
Upvotes: 1