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?
Related
I am trying to create a AppServiceConnection but when doing so I get a Incomplete Type not allowed
I verified the header file is imported for that class as mention in other stackoverflow questions.
I tried several different attempted to define the AppServieConnection. Am I putting in the wrong place?
The only way it worked was putting it above the main method.
Here is my code
#include "pch.h"
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <windows.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <tchar.h>
#include <stdio.h>
#include <ppltasks.h>
#include <appmodel.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <ppltasks.h>
#include <windows.h>
#include <appmodel.h>
#include <malloc.h>
#include <stdio.h>
#include <assert.h>
using namespace winrt;
using namespace concurrency;
using namespace Windows::Foundation;
using namespace std;
using namespace Windows::ApplicationModel::AppService;
AppServiceConnection connection_worked; // defining it here works but cannot call any methods from it
int main()
{
init_apartment();
Uri uri(L"http://aka.ms/cppwinrt");
printf("Hello, %ls!\n", uri.AbsoluteUri().c_str());
}
class NewClass {
private:
Windows::ApplicationModel::AppService::AppServiceConnection connection{nullptr};
Windows::ApplicationModel::AppService::AppServiceConnection connection2;
Windows::ApplicationModel::AppService::AppServiceConnection connection3 = nullptr;
AppServiceConnection connection4;
AppServiceConnection connection5 = nullptr;
AppServiceConnection connection6 = AppServiceConnection();
IAsyncAction InitializeAppServiceConnection() {
}
public:
NewClass() {
}
};
The error is clear, you are missing AppServiceConnection header file.
Please add the header file
#include <winrt/Windows.ApplicationModel.AppService.h>
This is my main.cpp
#include <iostream>
#include <unordered_map>
#include <string>
#include "myclass.h"
int main(int argc, const char * argv[]) {
any a = myclass::returnthis();
unordered_map<string, any>* hmap = any_cast<unordered_map<string, any>*>(a);
return 0;
}
this is myclass.h
#ifndef myclass_h
#define myclass_h
#include <any>
using namespace std;
class myclass {
public:
static any returnthis();
};
#endif /* myclass_h */
and this is myclass.cpp
#include "myclass.h"
#include <any>
#include <unordered_map>
#include <string>
any myclass::returnthis() {
return new unordered_map<string, any>();
}
Now, any_cast will report bad any cast. I am working on macOS and I have tried Xcode and CLion.
However, if I put the implementation of the function inside the header file, the problem vanishes. Why?
I'm new to c++ and was having problem with doing some inheritance, does any one know why im getting this error? (its the only error I'm getting when compiling).
I compiled with g++ -o. Also I'm so sorry in advance if I'm doing a lot of things wrong here, I'm very new to c++. >_<
Please let me know how I can make my code better or more efficient.
computer.h
#ifndef RPS_H
#define RPS_H
#include <iostream>
#include <string>
#include <stdio.h>
class Computer
{
public:
Computer(std::string);
~Computer();
char charc;
};
#endif
human.h
#ifndef HUMAN_H
#define HUMAN_H
#include <iostream>
#include <string>
#include <stdio.h>
class Human
{
public:
Human(std::string);
~Human();
char charh;
};
#endif
referee.h
#ifndef REFEREE_H
#define REFEREE_H
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
class Referee : public Human{
public:
Referee();
~Referee();
bool Winneris();
};
#endif
Computer.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "computer.h"
using namespace std;
Computer::Computer(string char_c)
{
}
Computer::~Computer()
{
}
Human.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
using namespace std;
Human::Human(string char_h){
char_h=charh;
cout<<"r/p/s?"<<endl;
cin>>charh;
}
Human::~Human()
{
}
Referee.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "referee.h"
using namespace std;
Referee::Referee(){
}
bool Referee::Winneris(){
if (charh=='r'){
cout<<"draw"<<endl;
}
else if(charh=='p'){
cout<<"Victory!"<<endl;
}
else if(charh=='s')
{
cout<<"Defeat"<<endl;
}
return 0;
}
Referee::~ReReferee(){
}
main.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
#include "computer.h"
#include "referee.h"
using namespace std;
string char_h;
string char_c;
// main program
int main()
{
Human *round1h;
round1h = new Human(char_h);
Computer *round1c;
round1c = new Computer(char_c);
Referee *round1r;
round1r = new Referee();
round1r -> Winneris();
}
When you have written the parameterized constructor in respective classes. You have created the class objects, which call the default constructor which takes no parameter.
You have to define the default constructor as well in your respective classes.
Human::Human()
{}
Computer::Computer()
{}
Referee::Referee()
{}
Constructor types
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.
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").