Class definition and utilization errors - c++

I am getting errors with the following code. The errors are incomplete type is not allowed and use of undefined type 'mGame'.
header.h:
//--Libraries
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//--Classes
class mGame;
Game.cc:
#include "header.h"
class mGame
{
private:
public:
bool intro();
};
Intro.cc:
#include "header.h"
bool mGame::intro() //--Line 3
{
printf("|-----------------------------|\n");
printf("\n Welcome to the Guessing Game!\n");
printf("\n|-----------------------------|\n");
return false;
}
The errors are both on line 3 of intro.cc. I tried finding a solution, but I couldn't for what I am doing.

header.h doesn't know any definitions of game.cc, you tell header.h only, that there is a class mGame. rename game.cc to game.h and include it into header.h and delete the line "class mGame;"

To be able to use mGame from Intro.cc, you have to move the class declaration into header.h (or into some other header file that you include from Intro.cc).
Having a forward declaration in header.h is not enough (that's what is meant by "incomplete type is not allowed").

Related

PImpl with C++: Why does the code not work?

I've solved the problem by putting #include "stdafx.h" (this statement is missed in the original question, sorry for that) BEFORE #include "PImplTest.h" instead of AFTER it.
But I'm still confused why it cannot stay after it.
// stdafx.h
#include <tinyxml2.h>
#include <queue>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <string>
#include <memory>
#include <assert.h>
----------------------- the original question --------------------------------
I'm trying to use the PImpl pattern in C++. The header file is:
// PImplTest.h
#pragma once
#include <memory>
class PImpl
{
private:
class Impl;
std::unique_ptr<Impl> m_impl;
};
What confuses me is, if I implement the Impl class in the corresponding cpp file of the header like this:
// PImplTest.cpp
#include "PImplTest.h"
#include "stdafx.h" // this statement was missed in my first post
class PImpl::Impl // error C2079 'Impl' uses undefined class 'PImpl'
{ // error C2653 'PImpl':is not a class or namespace name
};
// main.cpp
#include "stdafx.h" // this statement was missed in my first post
#include "PImplTest.h"
int main()
{
return 0;
}
VS reports the 2 errors in the code comments above. However, if it is implemented in a file like this:
// main.cpp
#include "PImplTest.h"
class PImpl::Impl
{
};
int main()
{
return 0;
}
Then nothing goes wrong.
What's the problem? How to fix it?

identifier "test" is undefined; What does this mean in this context

I am writing a weather station for a raspberry. I'm always getting the error message identifier "test" is undefined.
I've already tried to use no external class with a little example and this works perfectly. Now I'm trying to create an object test, but this doesn't work. I am always getting the error message:
E0020 identifier "test" is undefined
main.cpp:
#include <wiringPi.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <errno.h>
#include <stdlib.h>
include "MeasureWeather.h"
int main(void)
{
MeasureWeather test;
while (1)
{
test._setSensorPin(DHT_PIN);
}
return 0;
}
MeasureWeather.h:
#ifndef MeasureWeather
#define MeasureWeather
#include <wiringPi.h>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <stdlib.h>
#include <cstring>
class MeasureWeather
{
public:
//setter for sensor pin
void _setSensorPin(uint8_t pin);
private:
uint8_t sensorPin;
};
#endif // !MeasureWeather
MeasureWeather.cpp:
include "MeasureWeather.h"
void MeasureWeather::setSensorPin(uint8_t pin)
{
_sensorPin = pin;
}
I hope somebody can help me with my issue. Thank you!
You have this at the top of your header file, as part of the include guard:
#define MeasureWeather
MeasureWeather is the name of your class. By defining it as a macro, you hide the class name. Thus the line
MeasureWeather test;
expands to
test;
which would be a reference to something called test, not a declaration.
Use a different identifier for your #include guard.

C++ 'vector' was not declared in this scope

I'm getting an error everytime I compile the function.cpp file saying that stocks and newStock are not declared in this scope. I'm trying to use a struct inside a vector. Thanks for the help.
This is the main.cpp file
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;
struct Stocks
{
int one;
int two;
int three;
};
vector<Stocks> portfolio;
#include "testProject2.h"
int main()
{
buyStock(portfolio);
}
This is the header file.
#include <iostream>
void buyStock(vector<Stocks>& Portfolios);
This is the function.cpp file
#include <iostream>
#include <vector>
#include "testProject2.h"
void buyStock(vector<Stocks>& Portfolios)
{
Stocks newStock;
newStock{1,2,3};
Portfolios.push_back(newStock);
}
Your function.cpp file has no way to know what the Stocks struct is. Define it in the header file:
struct Stocks {
int one;
int two;
int three;
};
And remove its definition from main.cpp.
Also in your header file, you need
#include <vector>
and refer to vector parameter as std::vector<Stocks> &Portfolios (better than using namespace std;)
Your initialization syntax newstock{1,2,3} looks incorrect too.
You use vector in your header file without it being defined.
Try changing the header file to this:
#include <vector>
#include <Stocks.h> // name of .h file where Stocks is defined
void buyStock(std::vector<Stocks>& Portfolios);
// OR
using namespace std::vector;
void buyStock(vector<Stocks>& Portfolios);

ISO forbids declaration of "identifier" with no type.

Ok I know there are millions of variations of this particular problem, and I have tried (desperately) to go through them all and see if they apply, to no avail.
Currently I'm trying to declare a deque in a header file, and the damn thing wont let me due to the error mentioned. The same thing happens to me in a lot of my projects, and I think its just something basic I'm lacking in my knowledge of c++ class syntax.
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include "Card.h"
#include "random.h"
using namespace std;
void createloop();
int get_option();
deque <Card> make_new_deck();
deque <Card> load_new_deck();
int main()
{
createloop();
return 0;
}
I havn't shown the rest of the file for clarities sake, and im pretty confident it isnt the problem. The error appears in Card.h:
Card.h
#ifndef CARD_H
#define CARD_H
class Card
{
public:
Card();
deque<string> param_name_deque;
deque<double> param_value_deque;
virtual ~Card();
protected:
private:
};
#endif // CARD_H
card.cpp
#include "Card.h"
Card::Card()
{
//ctor
}
Card::~Card()
{
//dtor
}
To anyone who can help - thanks in advance! Ill be very happy when I understand whats wrong here!!!
You have to include both std::deque and std::string in your header file card.h
#include <string>
#include <deque>
Meanwhile,
deque<string> param_name_deque;
deque<double> param_value_deque;
should be
std::deque<std::string> param_name_deque;
std::deque<double> param_value_deque;
You need to specify the namespace in card.h when you declare the param_name_deque and param_value_deque:
std::deque<std::string> param_name_deque;
std::deque<double> param_value_deque;
and include the proper headers:
#include <string>
#include <deque>
I would avoid using namespace std, it may seem convenient but it will eventually cause you problems.

c++ Why do I get "does not name a type" error?

In my main class I have:
#include "main.h"
outPut O;
int main(){
...
}
where the main.h file has #include "outPut.h"
the "outPut.h" line has:
#ifndef OUTPUT_H
#define OUTPUT_H
#include <iostream>
#include <fstream>
#include "properties.h"
#include "particles.h"
class outPut{
public:
outPut();
std::ofstream file;
void show(lipid * l);
};
#endif
and the outPut.cpp:
#include "outPut.h"
outPut::outPut(){
}
When I compile this I get the error:
main.cpp:3: error: ‘outPut’ does not
name a type
Why so?
Thanks...
Edit, found it. main.h wasn't saved and the #include "outPut.h" was canceled.
You need to #include "outPut.h" in main.cpp.
grep for OUTPUT_H in all source files. You might have inadvertently defined the include guard in some other header which is included earlier than outPut.h.