CPP Header file struct error - c++

here is my header level.h:
#ifndef LEVEL_H_
#define LEVEL_H_
#include <string>
#include <map>
#include <queue>
#include <list>
typedef struct {
std::string title;
int stepsAmount;
std::queue <std::list <char> > steps;
std::queue <std::map <std::string, int> > stepsOptions;
} Level;
std::string getCurrentStepExpression(Level* level);
#endif
And my level.cpp:
#include "level.h"
std::string getCurrentStepExpression(Level* level) {
std::string result;
if (level)
result = level->stepExpressions.front();
return result;
}
Everything seems okay, but compiller says:
..\level.cpp: In function 'std::string getCurrentStepExpression(Level*)':
..\level.cpp:12:19: error: 'struct Level' has no member named 'stepExpressions'
why it doesn't see my struct's fields?

you need to add a stepExpressions member or stepExpressions() method to your struct Level and pass in some index to lookup in steps.
struct Level {
std::string title;
int stepsAmount;
std::queue <std::list <char> > steps;
std::queue <std::map <std::string, int> > stepsOptions;
/* add the method stepExpressions to your class */
std::list& stepExpressions(int step);
};

stepExpressions is not part of your struct.
Try adding it.

Related

Explicit instantion of boost multi_index container

First I would like to show the working code and then explain, how i want to change things. This is simple boost multi_index example:
//main.cpp
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <string>
struct employee
{
int id;
std::string name;
employee(int id, const std::string& name) :id(id), name(name){}
bool operator<(const employee& e)const{ return id<e.id; }
};
typedef boost::multi_index::multi_index_container<
employee,
boost::multi_index:: indexed_by<
// sort by employee::operator<
boost::multi_index:: ordered_unique< boost::multi_index:: identity<employee> >,
// sort by less<string> on name
boost::multi_index::ordered_non_unique<boost::multi_index::member<employee, std::string, &employee::name> >
>
> employee_set;
int main()
{
employee_set es;
es.insert(employee(0, "Bob"));
}
Imagine if main.cpp is another module, without boost dependency. I want to udnerstand how to:
include some header file with boost multiindex container class being forward declared into main.cpp
define multiindex container of employees in additional .cpp file
I have tried tons of variants, but none if this works. Is it possible to create something like this?
//notmain.cpp
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include "notmain.h"
typedef boost::multi_index::multi_index_container<
employee,
boost::multi_index::indexed_by<
// sort by employee::operator<
boost::multi_index::ordered_unique< boost::multi_index::identity<employee> >,
// sort by less<string> on name
boost::multi_index::ordered_non_unique<boost::multi_index::member<employee, std::string, &employee::name> >
>
> employee_set;
Now comes h.file I need to fill with forward declaration (or explicit initiation) of container. I may be misunderstanding these terms, but I am new to c++ and boost.
//notmain.h
#include <string>
/*
Some how here I need forward declaration or explicit initiation of boost container
class employee_set ???
*/
struct employee
{
int id;
std::string name;
employee(int id, const std::string& name) :id(id), name(name){}
bool operator<(const employee& e)const{ return id<e.id; }
};
This is final goal. I want to remind that main.cpp is imagined to be .cpp of another module, without boost dependency.
//main.cpp
#include "notmain.h"
int main()
{
employee_set es;
es.insert(employee(0, "Bob"));
}
If the type is part of a class' visible interface then any headers that class is dependent on have to be included, no way around that. If you really don't want it to be part of the visible interface consider using the pImpl idiom:
Public header
#if !defined(MYCLASS_PUBLIC_H_)
#define MYCLASS_PUBLIC_H_
struct MyClassImpl;
class MyClass {
MyClassImpl * pImpl;
public:
void SomeOperation();
};
#endif
Implementation header:
#if !defined(MYCLASS_IMPL_H_)
#define MYCLASS_IMPL_H_
#include <private_type.h>
#include "MyClass.h"
struct MyClassImpl
{
void Operation();
private:
SomePrivateType member;
};
#endif
Implementation file:
#include "MyClassImpl.h"
void MyClass::SomeOperation()
{
pImpl->Operation();
}
void MyClassImpl::Operation()
{
// do something with 'member'
}
Code that only sees the public interface:
#include "MyClass.h"
void foo()
{
MyClass inst;
inst.SomeOperation();
}

shared_ptr template argument invalid

I am trying to have a static method return a shared_ptr.
It is not compiling and is giving template argument 1 is invalid.
I can not figure out why this is.
Also, stack overflow says my post is mostly code and that I should add more detail. I don't know why this is, as being concise never hurt anyone. My problem is clear cut and and can be detailed easily.
Compiler error
src/WavFile.cpp:7:24: error: template argument 1 is invalid
std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string filename)
WavFile.cpp
#include "WavFile.h"
#include "LogStream.h"
#include "assert.h"
using namespace WavFile;
std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string filename)
{
ifstream infile;
infile.open( filname, ios::binary | ios::in );
}
WavFile.h
#pragma once
#ifndef __WAVFILE_H_
#define __WAVFILE_H_
#include <fstream>
#include <vector>
#include <memory>
namespace WavFile
{
class WavFile;
}
class WavFile::WavFile
{
public:
typedef std::vector<unsigned char> PCMData8_t;
typedef std::vector<unsigned short int> PCMData16_t;
struct WavFileHeader {
unsigned int num_channels;
unsigned int sample_rate;
unsigned int bits_per_sample;
};
static std::shared_ptr<WavFile> LoadWavFromFile(std::string filename);
private:
WavFile(void);
private:
WavFileHeader m_header;
PCMData16_t m_data16;
PCMData8_t m_data8;
};
#endif
Change the namespace name to something else. Now it clashes with the class' name, since you are using namespace WavFile;. Simple example that illustrates the error:
#include <iostream>
#include <memory>
namespace X // changing this to Y makes the code compilable
{
class X{};
}
using namespace X; // now both class X and namespace X are visible
std::shared_ptr<X> v() // the compiler is confused here, which X are you referring to?
{
return {};
}
int main() {}
If you insist to have the namespace named as the class, then get rid of the using namespace X; and qualify the type, std::shared_ptr<WafFile::WavFile>.

field ‘value’ has incomplete type

I have a C++ interdependence problem and i can not understand where the problem is...
Here are my headers:
json.array.h
#ifndef __JSON_ARRAY__
#define __JSON_ARRAY__
#include "json.object.h"
class JSON_OBJECT;
/* JSON_ARRAY */
class JSON_ARRAY {
int size;
custom_list<JSON_OBJECT> * container;
...
};
#endif
json.object.h
#ifndef __JSON_OBJECT__
#define __JSON_OBJECT__
#include "hash.h"
#include "elem_info.h"
#include "json.type.h"
class JSON_TYPE;
class elem_info;
/* JSON_OBJECT */
class JSON_OBJECT {
custom_list<elem_info> *H;
int HMAX;
unsigned int (*hash) (std::string);
...
};
#endif
json.type.h
#ifndef __JSON_TYPE__
#define __JSON_TYPE__
#include "json.object.h"
#include "json.array.h"
class JSON_OBJECT;
class JSON_ARRAY;
class JSON_TYPE {
JSON_ARRAY * _JSON_ARRAY_;
JSON_OBJECT * _JSON_OBJECT_;
std::string _JSON_OTHER_;
std::string _JSON_TYPE_;
...
};
#endif
elem_info.h
#ifndef __ELEM_INFO__
#define __ELEM_INFO__
#include "json.type.h"
class JSON_TYPE;
class elem_info {
public:
std::string key;
JSON_TYPE value;
...
};
#endif
main.cpp
#include <iostream>
#include <string>
#include "custom_list.h" // it inculdes cpp also
#include "json.type.h"
#include "elem_info.h"
#include "json.object.h"
#include "json.array.h"
#include "json.type.cpp"
#include "elem_info.cpp"
#include "json.object.cpp"
#include "json.array.cpp"
int main()
{
JSON_ARRAY * root = new JSON_ARRAY;
JSON_OBJECT obj;
JSON_OBJECT obj1;
JSON_OBJECT * obj2 = new JSON_OBJECT;
JSON_TYPE * type = new JSON_TYPE;
...
}
When i try to compile my code, i have this error:
elem_info.h:10:15: error: field ‘value’ has incomplete type
JSON_TYPE value;
It looks like it cant find JSON_TYPE. I cant understand where is the problem.
You have a forward declaration here
class JSON_TYPE;
class elem_info {
public:
std::string key;
JSON_TYPE value;
...
};
But value is an instance of JSON_TYPE. You can only forward declare if you have members that are pointers or references, not actual instances.
In fact, since you have a full include before that forward declaration, you don't need the forward declaration at all, and as I said it wouldn't help you anyway. You'd be fine with:
#ifndef __ELEM_INFO__
#define __ELEM_INFO__
#include "json.type.h"
class elem_info {
public:
std::string key;
JSON_TYPE value;
...
};
#endif
You can't do:
class JSON_TYPE;
class elem_info {
public:
std::string key;
JSON_TYPE value;
...
};
JSON_TYPE is an incomplete type. You can have a pointer or reference but no actual instance since the compiler does not know what it is.
json.type.h includes json.array.h which includes json.object.h which includes json.type.h.
That can't work.

C++ Undeclared Identifier (but it is declared?)

I'm pretty sure I've included the qanda class, but when I try to declare a vector that contains it or a class of that type I get an error saying that qanda is undefined. Any idea what the problem might be?
bot_manager_item.h
#pragma once
#include "../bot_packet/bot_packet.h"
#include <vector>
class bot_manager_item;
#include "qanda.h"
#include "bot_manager.h"
class bot_manager_item
{
public:
bot_manager_item(bot_manager* mngr, const char* name, const char* work_dir);
~bot_manager_item();
bool startup();
void cleanup();
void on_push_event(bot_exchange_format f);
bool disable;
private:
void apply_changes();
bot_manager *_mngr;
std::string _name;
std::string _work_dir;
std::string _message;
std::string _message_copy;
std::vector<qanda> games;
qanda test;
char _config_full_path[2600];
};
qanda.h
#ifndef Q_AND_A
#define Q_AND_A
#include "users.h"
#include "..\bot_packet\bot_packet.h"
#include "bot_manager.h"
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <fstream>
class qanda
{
public:
qanda(bot_manager * manager, std::string name, std::string directory);
~qanda(){};
void room_message(std::string username, std::string user_message);
void timer_tick();
private:
// data members
std::string question;
std::string answer;
std::string directory;
std::string command_prefix;
std::string name;
Users users;
std::map <std::string, std::string> questions_and_answers;
int time_per_question; // seconds
int time_between_questions; // seconds
int timer; // milliseconds
bool is_delayed;
bool is_playing;
bot_manager * manager;
// functions
void new_question();
void send_message(std::string msg);
void announce_question();
void load_questions();
};
#endif
Solved: I ended up refactoring the code in such a way as to avoid the use of bot_manager within the qanda class.
I suspect a circular #include problem. Is it possible qanda.h indirectly includes bot_manager_item.h?
It looks like you may be able to reduce header dependencies by using a forward declaration
class bot_manager;
instead of #include "bot_manager.h" in one or both of your posted header files.

conflicting declaration when filling a static std::map class member variable

I have a class with a static std::map member variable that maps chars to a custom type Terrain. I'm attempting to fill this map in the class's implementation file, but I get several errors. Here's my header file:
#ifndef LEVEL_HPP
#define LEVEL_HPP
#include <bitset>
#include <list>
#include <map>
#include <string>
#include <vector>
#include "libtcod.hpp"
namespace yarl
{
namespace level
{
class Terrain
{
// Member Variables
private:
std::bitset<5> flags;
// Member Functions
public:
explicit Terrain(const std::string& flg)
: flags(flg) {}
(...)
};
class Level
{
private:
static std::map<char, Terrain> terrainTypes;
(...)
};
}
}
#endif
and here's my implementation file:
#include <bitset>
#include <list>
#include <map>
#include <string>
#include <vector>
#include "Level.hpp"
#include "libtcod.hpp"
using namespace std;
namespace yarl
{
namespace level
{
/* fill Level::terrainTypes */
map<char,Terrain> Level::terrainTypes['.'] = Terrain("00001"); // clear
map<char,Terrain> Level::terrainTypes[','] = Terrain("00001"); // clear
map<char,Terrain> Level::terrainTypes['\''] = Terrain("00001"); // clear
map<char,Terrain> Level::terrainTypes['`'] = Terrain("00001"); // clear
map<char,Terrain> Level::terrainTypes[178] = Terrain("11111"); // wall
(...)
}
}
I'm using g++, and the errors I get are
src/Level.cpp:15: error: conflicting declaration ‘std::map, std::allocator > > yarl::level::Level::terrainTypes [46]’
src/Level.hpp:104: error: ‘yarl::level::Level::terrainTypes’ has a previous declaration as ‘std::map, std::allocator > > yarl::level::Level::terrainTypes’
src/Level.cpp:15: error: declaration of ‘std::map, std::allocator > > yarl::level::Level::terrainTypes’ outside of class is not definition
src/Level.cpp:15: error: conversion from ‘yarl::level::Terrain’ to non-scalar type ‘std::map, std::allocator > >’ requested
src/Level.cpp:15: error: ‘yarl::level::Level::terrainTypes’ cannot be initialized by a non-constant expression when being declared
I get a set of these for each map assignment line in the implementation file. Anyone see what I'm doing wrong? Thanks for your help.
You can initialize static members outside of functions, but you can't perform arbitrary operations.
You could use a function to initialize the members:
namespace {
std::map<char, Terrain> initTerrainTypes() {
std::map<char, Terrain> m;
m['.'] = Terrain("00001");
// ...
return m;
}
}
map<char,Terrain> Level::terrainTypes = initTerrainTypes();
Or you could use initialization utilities like Boost.Assign:
map<char,Terrain> Level::terrainTypes = boost::assign::map_list_of
('.', Terrain("00001"))
// ...
(178, Terrain("11111"));