Home > Enterprise >  'Type' does not refer to a value
'Type' does not refer to a value

Time:06-29

I've checked other questions with similar errors, and they didn't seem comparable to the issue that I'm running into. I'm trying to instantiate an object and assign it in a constructor initialization list.

However, on line 11, I'm getting an error saying 'Enemy' does not refer to a value. Furthermore, also on line 11, the log is giving me: error: expected primary-expression before 'Goblin'

#include "Room.h"
#include <iostream>
#include "Enemy.h"
#include "Chest.h"
#include <string>

Room::Room(Enemy enem_obj)
    :symbol{"_ "}, enemy{enem_obj}{
}
Room::Room()
    : Room{Enemy Goblin{"Goblin"}}{
}

void Room::print(std::ostream& os){
    os << symbol;
}
#ifndef ROOM_H
#define ROOM_H
#include "I_Mapsquare.h"
#include "Enemy.h"
#include "Chest.h"
#include <string>

class Room : public I_Mapsquare
{
private:
    std::string symbol;
    Enemy enemy;
    Chest chest;
    
public:
    Room(Enemy enem_obj);
    Room();
    virtual ~Room() = default;
    virtual void print(std::ostream& os) override;
};

#endif // ROOM_H
#ifndef ENEMY_H
#define ENEMY_H
#include "I_Player.h"


class Enemy : public I_Player
{
private:
    static constexpr const char* def_name = "Enemy"; 
    static constexpr double def_hp = 20.0;
    static constexpr double def_def = 3.0;
    static constexpr double def_str = 2.0;
    static constexpr double def_dex = 1.0;
    static constexpr double def_wepdmg = 4.0;
    
public:
    Enemy(std::string name = def_name, double hp = def_hp, double str = def_str, double defense = def_def, double dex = def_dex, double wepdmg = def_wepdmg);
    Enemy(std::string name);
    virtual ~Enemy() = default;
    
    virtual void attack(I_Player& obj) override;

};

#endif // ENEMY_H
#include "Enemy.h"
#include <string>
  
Enemy::Enemy(std::string name, double hp, double str, double defense, double dex, double wepdmg)
    : I_Player {name, hp, str, defense, dex, wepdmg}
{
}
Enemy::Enemy(std::string name)
    : Enemy{name, 20.0, 3.0, 2.0, 1.0, 4.0}
{
}
    

void Enemy::attack(I_Player& obj){
    obj.takedmg(*this);
}

I'm sure this code is beyond horrendous, as this is the first independent program I've made, and I just wanted to try to make some sort of functional program using cpp. I'm also fairly certain that I'm trying to do some sort of horribly moronic operation here and that is why this isn't compiling. Pointing me in the right direction or to some material that would help me understand what is going on here would be helpful. Thank you all for your time!

CodePudding user response:

Not sure why you are trying to name this object, just

Room::Room()
    : Room{Enemy{"Goblin"}}{
}

Temporaries (like Enemy{"Goblin"}) are objects without names.

CodePudding user response:

The problem is that Enemy Goblin{"Goblin"} is a declaration and a declaration is not allowed inside the parenthesis () of a mem-initializer. This can be seen from base.class.init:

ctor-initializer:
    : mem-initializer-list
mem-initializer-list:
    mem-initializer ...opt
    mem-initializer-list , mem-initializer ...opt
mem-initializer :
//-----------------------vvvvvvvvvvvvvvv------------->note this part
    mem-initializer-id ( expression-list... opt )
    mem-initializer-id braced-init-list
mem-initializer-id:
    class-or-decltype
    identifier

(emphasis mine)

As we can see above, the construct inside the parenthesis () should be an expression list but since you're providing a declaration, you get the mentioned error.

To solve this, you can skip naming the object:

Room::Room()
//---------vvvvvvvvvvvvvvv---->ok now as this is an expression
    : Room{Enemy{"Goblin"}}{
}
  • Related