I want to create method with an argument which links to Enemy which is declared later.
Here is my code:
#include <iostream>
#include <vector>
using namespace std;
class Weapon{
public:
int atk_points;
string name;
string description;
void Attack(Entity target){
};
};
class Armor{
public:
int hp_points;
string name;
string description;
int block_chance;
};
class Entity{
public:
int hp;
int atk;
string name;
vector<Weapon> weapons;
vector<Armor> armors;
};
I tried to search for answers, but nothing I found was helpful.
Here's error log:
prog.cpp:9:15: error: ‘Entity’ has not been declared
void Attack(Entity target){
The problem is that the compiler doesn't know what Entity is at the point where you have used as a parameter type. So you need to tell the compiler that Entity is a class type.
There are 2 ways to solve this both of which are given below:
Method 1
To solve this you need to do 2 things given below:
Provide a forward declaration for the class Entity.
Make the parameter of Attack to be a reference type so that we can avoid unnecessary copying the argument and also since we're providing a member function's definition instead of just declaration.
class Entity; //this is the forward declaration
class Weapon{
public:
int atk_points;
string name;
string description;
//------------------------------v------------>target is now an lvalue reference
void Attack(const Entity& target){
};
};
Working demo
Method 2
Another way to solve this is that you can provide just the declaration for the member function Attack' inside the class and then provide the definition after the class Entity's definition as shown below:
class Entity; //forward declaration
class Weapon{
public:
int atk_points;
string name;
string description;
//------------------------------v----------->this time using reference is optional
void Attack(const Entity& target); //this is a declaration
};
//other code here as before
class Entity{
public:
int hp;
int atk;
string name;
vector<Weapon> weapons;
vector<Armor> armors;
};
//implementation after Entity's definition
void Weapon::Attack(const Entity& target)
{
}
Working demo
You can't.
You must declare it earlier. You can, however, define it later, in some circumstances.
To forward declare a class, write this before it is used:
class Entity;
In c++, such code cannot compile:
class A {
void fooa(B) {}
};
class B {
void foob(A) {}
};
However, such code can be compiled, we can change code in the way:
class A { };
class B { };
void fooa(A *, B) {}
void foob(B *, A) {}
It works and nothing is recursive.
So, I don't think change to reference is a good idea. The directly way to do that is just to use some trick. For example, change Entity to auto. Like that: void Attack(auto target).
What's more, with c++20, you can define a concept attackable and make Entity is attackable, I like it much.
Related
im learing c++.I was goofing around trying new stuff and wanted to use an object class sub in class object.But i was getting error saying that the object of class sub not in not defined.I know how to solve this issue i just have to move class sub above class object so that the compiler knows that there is a class called sub.
But I feel like this will get annoying as my code grows bigger and bigger so i tried forward declaring class like we do for function prototyping.But this doesn't work as it gives me this error -
'object::thing' uses undefined class 'sub'
Here is the code -
#include <iostream>
#include <vector>
class sub;
class object;
class object
{
private:
sub thing;
int ray;
public:
void set(int n);
void get() const;
};
class sub
{
public:
int num;
public:
void set_num(int n);
void get_num() const;
};
int main()
{
object ray;
ray.set(4);
ray.get();
}
Can you guys help me out??
thanks
When a type is used in c++, at a minimum that type needs to have been declared previously.
However, in some cases, the type needs to be defined (complete) as well.
class A;
class B {
A a; // error, because A needs to be defined
};
In other cases, the type only needs to be declared (i.e. it can be incomplete):
class A;
class B {
A *a; // fine, because A only needs to be declared
};
If a type needs to be defined when it's used (as in your case), then you have no choice but to define it before hand. You could put the definition in a header file, but that file still needs to be included before the type is used.
I have a private struct in my class Menu that contains menu item attributes. one of those attributes is a pointer to a function. It seems as though what I have written works when I forward declare functions outside the class, but having functions defined outside the class seems like very bad practice.
#pragma once
#include <string>
//forward declaration outside the class
void completelyRandom();//does not throw error
class Menu
{
private:
typedef void(*Menu_Processing_Function_Ptr)();
struct MenuItem
{
unsigned int number;
const char * text;
Menu_Processing_Function_Ptr p_processing_function;
};
MenuItem menu[] =
{
{1, "Completely random", completelyRandom}
};
public:
Menu();
~Menu();
};
And here is some code that throws the error but is closer to what I hope is possible:
#pragma once
#include <string>
class Menu
{
private:
typedef void(*Menu_Processing_Function_Ptr)();
struct MenuItem
{
unsigned int number;
const char * text;
Menu_Processing_Function_Ptr p_processing_function;
};
MenuItem menu[1] =
{
{1, "Completely random", completelyRandom}
};
public:
Menu();
//declaration within the class
void completelyRandom();//does throw error
~Menu();
};
I have tried moving the completelyRadom() declaration all around within the Menu scope, but get the error
a value of type "void (Menu::*)()" cannot be used to initialize an entity of type "Menu::Menu_Processing_Function_Ptr"
Is there a way I can forward declare the function that would be within best practices? Or should I regret my poor design choices and start all over?
"Non-static member functions (instance methods) have an implicit parameter (the this pointer) which is the pointer to the object it is operating on, so the type of the object must be included as part of the type of the function pointer. " - https://en.wikipedia.org/wiki/Function_pointer
check this code for an example:
#pragma once
#include <string>
class Menu
{
private:
//By specifying the class type for the pointer -> 'Menu::' in this case
// we no longer have to forward declare the functions
//outside of the class
typedef void(Menu::*Menu_Processing_Function_Ptr)();
struct MenuItem
{
unsigned int number;
const char * text;
Menu_Processing_Function_Ptr p_processing_function;
};
MenuItem menu[1] =
{
{1, "Completely random", completelyRandom}
};
public:
Menu();
void completelyRandom(); // no longer throws an error
};
Lippman 5th
ISBN-13: 978-0321714114
Page 280-281, it says:
Making A Member Function a Friend
Rather than making the entire Window_mgr class a friend, Screen can
instead specify that only the clear member is allowed access. When we
declare a member function to be a friend, we must specify the class of
which that function is a member:
class Screen {
// Window_mgr::clear must have been declared before class Screen
friend void Window_mgr::clear(ScreenIndex);
// ... rest of the Screen class
};
Making a member function a friend requires careful structuring of our
programs to accommodate interdependencies among the declarations and
definitions. In this example, we must order our program as follows:
First, define the Window_mgr class, which declares, but cannot define, clear. Screen must be declared before clear can use the
members of Screen.
Next, define class Screen, including a friend declaration for clear.
Finally, define clear, which can now refer to the members in Screen.
The problem is: class Window_mgr has a data member that depends of class
Screen definition. See:
class Window_mgr {
public:
// location ID for each screen on the window
using ScreenIndex = std::vector<Screen>::size_type;
// reset the Screen at the given position to all blanks
void clear(ScreenIndex);
private:
std::vector<Screen> screens{Screen(24, 80, ' ')};
};
So it is impossible firstly define Window_mgr without defining Screen
previously!
And at the same time, it is impossible define Screen without we have
defined Window_mgr!!!
How can this problem be solved???
Is the book wrong?
I will paste here a code so that you can repeat the problem using a
minimal code:
#include <iostream>
#include <string>
#include <vector>
class A
{
friend void B::hello();
public:
A(int i) : number{i} {}
private:
void f() {
std::cout << "hello" << std::endl;
}
int number;
};
class B {
private:
std::vector<A> x{A(10)};
public:
void hello()
{
for(A &elem : x)
{
elem.f();
}
}
};
int main()
{
A x;
return 0;
}
If I compile this code, the result is:
error: use of undeclared identifier 'B'
friend void B::hello();
And if I invert the position (A <--> B), I have:
error: use of undeclared identifier 'A'
std::vector x{A(10)};
Is there a correct way to do that??
Thank you!
EDIT:
Thank you, Craig Young
Solution:
#include <iostream>
#include <string>
#include <vector>
class A;
class B {
private:
std::vector<A> x;
public:
B();
void hello();
};
class A
{
friend void B::hello();
public:
A(int i) : number{i} {}
private:
void f() {
std::cout << "hello" << std::endl;
}
int number;
};
B::B() : x{A(10)}
{
}
void B::hello()
{
for(A &elem : x)
{
elem.f();
}
}
int main()
{
return 0;
}
Conclusion:
the book is incomplete in that it doesn't expose the necessity of doing the forward declaration of class A firstly and the impossibility to do in-class initialization in this case.
I didn't notice that the problem was the A(10), not the vector! That is, we can use incomplete type A (only declaration, without definition) when we are using it as Template argument to vector (because it doesn't create A object itself) but we can not use incomplete type A when defining a object, for example: A(10);
For a start
Well, you're not following the guidance correctly.
First, define the Window_mgr class, which declares, but cannot define, clear. Screen must be declared before clear can use the members of Screen.
You must declare B before A.
Next, define class Screen, including a friend declaration for clear.
Now declare A with B::hello() as a friend.
Finally, define clear, which can now refer to the members in Screen.
B:hello() can use the private members of A.
This has been covered before here: C++ Forward declaration , friend function problem
You've added complications
Furthermore you want declarations of B to reference A. To achieve this you need to forward declare A so that B knows of its existence.
And it's important to be aware that you have only "partial" access to A. You cannot 'fully use' A in the declaration of B. So the following line in B is wrong.
//You're trying to create A when you only know it exists.
//You don't have a full definition of A yet.
std::vector<A> x{A(10)};
//Replace the above with...
std::vector<A> x;
Of course you'll have to find another way to initialise x.
Sample code
#include <iostream>
#include <vector>
class A;
class B
{
private:
std::vector<A> x;
public:
void hello();
};
class A
{
friend void B::hello();
public:
A(int i): number(i) {}
private:
void f() { std::cout << "hello" << std::endl; }
int number;
};
void B::hello()
{
for(A &elem : x)
{
elem.f();
}
}
int main()
{
A a{5};
return 0;
}
You have to have an earlier declaration, but not an earlier definition.
Adding
class A;
class B;
at the front tells the compiler that “A” and “B” refer to classes. That should be enough for it to reason out the rest.
I have decided learn some OOP and I started get rid of conditionals (I know in this program this in unnecessary but I have to start with simply example).
In class DirectConditions, I have my conditions and I would pass object of SnakeGame by reference to friend class DirectConditions's method and change value of some variables of SnakeGame in DirectConditions's method.
Error:
invalid use of incomplite type 'class DirectConditions::SnakeGame' snakeObj.snake[0].xCoor+=1;
Another Question:
May I take this occasion to ask: this way to get rid of conditionals(if's) is good? I would really appreciate help.
Providing the code:
snakeGame.hpp:
#ifndef _SNAKEGAME_HPP
#define _SNAKEGAME_HPP
#include<string>
#include"directConditions.hpp"
#include<map>
class SnakeGame{
public:
SnakeGame();
void mainLogic();
void check();
friend class DirectConditions;
private:
const int width,height;
int sizeOfSnake;
std::string direction;
std::map<std::string,DirectConditions*> directMap;
struct Snake{ int xCoor,yCoor; }snake[100];
enum Directions{up,down,left,right} directEnum;
void initializeMap();
};
SnakeGame::SnakeGame():width(500),height(500),sizeOfSnake(3){}
void SnakeGame::check(){
for(int i=sizeOfSnake;i>0;--i){
snake[i].xCoor=snake[i-1].xCoor;
snake[i].yCoor=snake[i-1].yCoor;
}
directMap[direction].goToDirection(this);
}
//private fun
void SnakeGame::initializeMap(){
directMap["right"]=new GoRight;
directMap["left"]=new GoLeft;
directMap["up"]=new GoUp;
directMap["down"]=new GoDown;
}
#endif
directConditions.hpp:
#ifndef _DIRECTCONDITIONALS_HPP
#define _DIRECTCONDITIONALS_HPP
#include"snakeGame.hpp"
class DirectConditions{
public:
class SnakeGame;
virtual void goToDirection(SnakeGame&)=0;
virtual ~DirectConditions(){};
};
class GoRight:public DirectConditions{
public:
void goToDirection(SnakeGame& snakeObj){
snakeObj.snake[0].xCoor+=1;
}
};
class GoLeft:public DirectConditions{
public:
void goToDirection(SnakeGame& snakeObj){
snakeObj.snake[0].xCoor-=1;
}
};
class GoUp:public DirectConditions{
public:
void goToDirection(SnakeGame& snakeObj){
snakeObj.snake[0].yCoor+=1;
}
};
class GoDown:public DirectConditions{
public:
void goToDirection(SnakeGame& snakeObj){
snakeObj.snake[0].yCoor-=1;
}
};
#endif
You should avoid including of headers by each other - that's why you're getting "invalid use of incomplete type".
Change those includes to forward declarations and move implementation of methods to cpp files.
I have a template class that when I instantiate in main doesn't have any issues but when i try instantiating the same in another class is giving issues. can someone please enlighten me on the solution for this
#include<iostream>
#include<string>
using namespace std;
template <class T>
class property {
public:
property(string name)
{
propertyName= name;
}
private:
T item;
string propertyName;
};
main()
{
property<int> myIntProperty("myIntProperty");
}
the above code compiles with out any issue.
but
#include<iostream>
#include<string>
using namespace std;
template <class T>
class property {
public:
property(string name)
{
propertyName= name;
}
private:
T item;
string propertyName;
};
class propertyHolder
{
property<int> myIntProperty("myIntProperty");
};
this code is not getting compiled.
giving me error like
main.cpp|19|error: expected identifier before string constant|
main.cpp|19|error: expected ',' or '...' before string constant|
Thanks,
Harish
property<int> myIntProperty("myIntProperty");
This is a function declaration, so it expects you to insert a default argument after identifying it, like string s = "myIntProperty".
Perhaps you want to initialize an object called myIntProperty,
property<int> myIntProperty {"myIntProperty"};
This can be done in C++11, but you can also initialize it in the constructor initializer list,
// Header
class propertyHolder {
public:
propertyHolder( string s );
private:
property<int> myIntProperty;
};
// Source
propertyHolder::propertyHolder( string s ) :
myIntProperty( s )
{
}
You wanted to declare field in class propertyHandler. That syntax is not working because you cannot declare a field and assing it value at the same spot.
You can delcare it, and initialise in constructor:
property<int> myIntProperty;
propertyHolder(): myIntProperty("name") {}
or with c++11 syntax:
property<int> myIntProperty{"name"};
or declare it static, and them declare like that:
static property<int> myIntProperty;
and after class declaration:
property<int> propertyHolder::myIntProperty("name");