Class inside a struct - c++

I am getting flustrated with two errors and I have absolutely no idea what is wrong.
#ifndef ListElements
#define ListElements
#include "RentalObjects.h"
struct RentalList{
ObjectBase* content;
RentalList* Next;
};
#endif
All the time I get this error:
Error 1 error C2143: syntax error : missing ';' before '*'
Error 2 error C4430: missing type specifier - int assumed.
The RentalObjects.h file features a declaration of the ObjectBase class, which looks as follows:
class ObjectBase{
protected:
char Make[16];
char Model[16];
int Year;
float PricePerDay;
Booking* Availability;
public:
void SetMake(char* value);
void SetModel(char* value);
void SetYear(int value);
void SetPrice(float value);
bool DisposeBookings();
bool Book(int Start,int End);
char* GetMake();
char* GetModel();
int GetYear();
float GetPrice();
~ObjectBase();
};
I'd be grateful for a tip.

When declaring pointers or references, you don't need the whole class/struct definition.
Instead of:
#include "RentalObjects.h"
struct RentalList {
ObjectBase* content;
RentalList* Next;
};
you could do:
class ObjectBase;
struct RentalList {
ObjectBase* content;
RentalList* Next;
};
this could get you out of a circular include, which might be what's causing your broblem

Related

C++ Class Methods Declaration Returns Error

I have recently started working with C++ classes and had just started when I reached an error. I have a "resource.h" file that contains the class definition of two classes: 'deck' and 'card'. I #included this file in another file, "card.cpp". In the card.cpp file I described all the methods/functions of the 'card' class. However on compilation I am getting the following the errors (fyi I am using the MinGW compiler for command-line):
card.cpp:3:29: error: ISO C++ forbids declaration of 'setCard' with no
type [-fp ermissive] card.cpp:3:1: error: prototype for 'int
Card::setCard(char, char)' does not matc h any in class 'Card'
resource.h:9:8: error: candidate is: void Card::setCard(char, char)
The "card.cpp" file:
#include "resource.h"
Card::setCard(char f, char s) {
face = f;
suit = s;
}
Card::Card (char face, char suit) {
setCard(face, suit);
}
Card::~Card () {}
The "resource.h" file:
typedef unsigned short int UINT;
class Card;
class Deck;
class Card {
public:
Card(char face, char suit);
~Card();
void setCard(char face, char suit);
char getFace() const { return face; }
char getSuit() const { return suit; }
private:
char face;
char suit;
};
class Deck {
public:
Deck();
~Deck();
Card getCard(UINT x);
private:
Card myCards[54];
};
What is causing this issue, and why in the world does the compiler think that "Card::setChard()" is an int
Card::setCard(char f, char s) {
face = f;
suit = s;
}
should be
void Card::setCard(char f, char s) {
face = f;
suit = s;
}
Some hints that helped me get to this amazing conclusion:
C++ forbids declaration of 'setCard' with no type
candidate is: void Card::setCard(char, char)
If you thought this was cryptic, hold on tight for when you get to templates. Compilers have a history of generating great error messages for them.

Uint8 - Missing Type specifier

I'm trying to write a singleton class to hold the state of inputs from the user (mouse/keyboard data). The SDL API returns keyboard data as Uint8 pointer array, however, why I try to create the Uint8 pointer, I get these errors at the line w/ the uint8:
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
I've used Uint8 as a data type without defining it before, so I'm not sure what is causing the issue here. Here is my code:
class InputState {
public:
InputState()
{};
~InputState()
{};
static InputState *getInputState(void)
{
static InputState *state = new InputState();
return state;
};
public:
Uint8 *keys;
struct MouseState
{
int LeftButtonDown;
int RightButtonDown;
int MiddleButtonDown;
int x;
int y;
MouseState ()
{
LeftButtonDown = 0;
RightButtonDown = 0;
MiddleButtonDown = 0;
x = 0;
y = 0;
}
};
MouseState *mouseState;
};
The type Uint8 is a typedef that is defined in one of the SDL header. If you want to use it, you need to include the SDL.h header in your file.
// You need this include if you want to use SDL typedefs
#include <SDL.h>
class InputState {
public:
InputState()
{};
~InputState()
{};
// ...
public:
Uint8 *keys;
// ...
};

Whats the errors not able to understand

i made a code but has errors and wan't able to solve them:-
the following errors are in one of mine header file(code shown below)
error C2143:syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Header file code:-
#ifndef _DEVICE_SOURCE_FICARD_HH
#define _DEVICE_SOURCE_FICARD_HH
#ifndef _FRAMED_SOURCE_HH
#include "FramedSource.hh"
#endif
#include <DeviceSource.hh>
typedef struct LtagBufferEntry
{
char *pBuffer;
struct LtagBufferEntry *pNext;
} LBufferEntry;
class FICardDeviceParameters {
public:
(RetEntry*)(*p_lm_lock_fn)( void *data ); //error at this line
void (*p_lm_unlock_fn)( void *data );
int nFICardFrameSize;
//%%% TO BE WRITTEN %%%
};
class DeviceSourceFICard: public DeviceSource {
public:
static DeviceSourceFICard* createNew(UsageEnvironment& env, FICardDeviceParameters fi_params,
DeviceParameters params);
protected:
DeviceSourceFICard(UsageEnvironment& env, FICardDeviceParameters fi_params, DeviceParameters params);
// called only by createNew(), or by subclass constructors
virtual ~DeviceSourceFICard();
private:
// redefined virtual functions:
virtual void doGetNextFrame();
private:
void deliverFrame();
private:
DeviceParameters fParams;
LBufferEntry *pData;
char * pRetData;
//int nFICardFrameSize;
FICardDeviceParameters fiParams;
};
#endif //_DEVICE_SOURCE_FICARD_HH
Defination of RetEntry:-
typedef struct tagRetBuffer
{
char *pBuffer;
int nDataLn;
} RetEntry;
void InitBufferHandling();
void TransferBuffer( void *pBuffer );
RetEntry *lm_lock_fn( void *data );
void lm_unlock_fn( void *data );
int initLm555Settings(void);
void play();
void afterPlaying(void*);
void init_play();
void StartRTPProcess(void);
How to fix them...
The problem is that the compiler does not know what RetEntry is in the expression:
(RetEntry*)(*p_lm_lock_fn)( void *data );
You must either include the header that provides the definition or else declare it in your current header. From the code below it seems to be a typedef to tagRetBuffer, so to declare it in the current translation unit you will need to declare the struct and the typedef:
struct tagRetBuffer;
typedef tagRetBuffer RetEntry;
Now, I would suggest that you avoid those typedef's altogether, in C++ the compiler will search for types if needed. You can just do:
struct RetEntry
{
char *pBuffer;
int nDataLn;
};
And then use RetEntry anywhere as if it was a typedef'ed name. Note that there are differences, but that in 99% of the cases you will not notice. And when you do notice it might even be to your advantage (for example, you cannot declare a typedef, so you need to declare the typedef'ed name and then redefine the typedef as per the first suggestion here).

class issue in C++

I have this in furniture.h:
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture();
virtual ~Furniture();
void setname(string name);
void setprice(double price);
int getprice();
string getname();
private:
string name;
int price;
protected:
static int NumberOfItems;
int Id;
}
and this in furniture.cpp
#include "furniture.h"
void Furniture::setname(string name) {
this->name = name;
}
string Furniture::getname()
{
return this->name;
}
void Furniture::setprice(double price) {
this->price = price;
}
int Furniture::getprice() {
return this->price;
}
int main() {
Furniture *model = new Furniture();
model->setname("FinalDestiny");
model->setprice(149.99);
cout<<"Model name: "<<model->getname()<<" - price = "<<model->getprice();
}
But I get some errors like:
Error 1 error C2628: 'Furniture' followed by 'void' is illegal (did you forget a ';'?) c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 2 error C2556: 'Furniture Furniture::setname(std::string)' : overloaded function differs only by return type from 'void Furniture::setname(std::string)' c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 3 error C2371: 'Furniture::setname' : redefinition; different basic types c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 5 error C2264: 'Furniture::setname' : error in function definition or declaration; function not called c:\final\facultate\poo\laborator 1\furniture.cpp 19 1 POO_lab
What am I doing wrong?
You are missing a ; at the end of the class definition in your header file.
// ...snipped...
protected:
static int NumberOfItems;
int Id;
}; // <-- here
You've forgotten a semicolon at the end of your class definition.
// ...
protected:
static int NumberOfItems;
int Id;
}; // <--
I hate that about C++ :)
Two things;
You're not ending your class definition with a ;, you need one at the end of furniture.h.
You've declared that there's a constructor and destructor, but neither is implemented in your .cpp file.

C++ "error: multiple types in one declaration" and "error: expected initializer before 'KeyType'"

When I run the following code...
#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED
#include <string>
typedef std::string KeyType;
class KeyedItem {
public:
KeyedItem() {}
KeyedItem(const KeyType& keyValue) : searchKey(keyValue) {}
KeyType getKey() const
{ return searchKey;
}
private:
KeyType searchKey; };
#endif // KEYEDITEM_H_INCLUDED
I get an error message "error: expected initializer before 'KeyType'"
I thought at first that this could be related to the declaring the string type so I changed it to the following to see if it would work...
#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED
#include <string>
//typedef std::string KeyType;
class KeyedItem
{
public:
KeyedItem() {}
KeyedItem(const std::string& keyValue) : searchKey(keyValue) {}
std::string getKey() const
{ return searchKey;
}
private:
std::string searchKey;
};
#endif // KEYEDITEM_H_INCLUDED
but I got the error "error: multiple types in one declaration" I have looked for errors for both of these errors and have found nothing that helps. I have gone over the class to make sure I have semi-colons where needed and I seem to have all of them.
I don't have a implementation file simply because I didn't need one, but could that be the problem?
This is just a class for a binary search tree. I am working in CodeBlocks using the GNU GCC compiler.
TreeNode.h
#ifndef TREENODE_H_INCLUDED
#define TREENODE_H_INCLUDED
#include "KeyedItem.h"
typedef KeyedItem TreeItemType;
class TreeNode
{
private:
TreeNode() {}
TreeNode(const TreeItemType& nodeItem,
TreeNode *left = NULL,
TreeNode *right = NULL) : item(nodeItem), leftChildPtr(left), rightChildPtr(right) {}
TreeItemType item;
TreeNode *leftChildPtr, *rightChildPtr;
friend class BinarySearchTree;
};
#endif // TREENODE_H_INCLUDED
you need to compile with g++ not gcc
Solved... Turns out I was missing a header include in my main.