Keep getting unordered_map compiler error [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Header:
#include <unordered_map>
#include "O.h"
#include "P.h"
using namespace std;
class O{
public:
O();
unordered_map<int,P>* X();
unordered_map<int,P>* Y();
private:
unordered_map<int,P>* b;
unordered_map<int,P>* a;
};
source:
#include "O.h"
#include "P.h"
#include <unordered_map>
using namespace std;
O::O(){
a= new unordered_map<int,P>();
b= new unordered_map<int,P>();
}
unordered_map<int,P>* O::X(){
return b;
}
unordered_map<int,P>* O::Y(){
return a;
}
Error is:
1>O.cpp(76): error : return value type does not match the function
type 1> return b;
1>O.cpp(80): error : return value type does not match the function
type 1> return a;
I'm going crazy trying to debug this....
EDIT: Intel Compiler v13

The code you have posted is valid C++, so the issue must lie elsewhere in your code. I would check the included headers. Here is an example with a valid declaration of P:
#include <unordered_map>
using namespace std;
class P{
public:
int a = 3;
};
class O{
public:
O();
unordered_map<int,P>* X();
unordered_map<int,P>* Y();
private:
unordered_map<int,P>* b;
unordered_map<int,P>* a;
};
O::O(){
a= new unordered_map<int,P>();
b= new unordered_map<int,P>();
}
unordered_map<int,P>* O::X(){
return b;
}
unordered_map<int,P>* O::Y(){
return a;
}
int main(){
O o;
auto map = o.X();
return 0;
}
ideone: http://ideone.com/Y4ydzj

Related

Incomplete type error when using overloaded constructor [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I've added a C++ class to help with simulations I'm running. It was working fine, until I added another constructor with different inititation parameters, after which the compiler complains about an 'incomplete type' error with the original constructor that was working before. CLion also complains it can't find a matching constructor, even though its the same one I've been using until now.
PS. I'm creating 2 separate instances of the class (1 in main, 1 in another class also initialised in main)...not sure if this is maybe an issue?
Here's the code:
SimEnv.hpp
#ifndef SIMENV
#define SIMENV
#include "ClassContainingSomeParams.hpp"
class SimEnv
{
private:
int p1, p2;
public:
SimEnv(ClassContainingParams); // This is the bad constructor
SimEnv(int, int);
};
#endif
SimEnv.cpp
#include "SimEnv.hpp"
/* This is the bad constructor */
SimEnv::SimEnv(ClassContainingSomeParams p) :
p1(p.getP1()), p2(p.getP2())
{}
SimEnv(SimEnv(int p1, int p2) : p1(p1), p2(p2)
{}
ClassContainingSomeParams.hpp
#ifndef CLASSCONTAININGSOMEPARAMS
#define CLASSCONTAININGSOMEPARAMS
#include "SimEnv.hpp"
ClassContainingSomeParams
{
public:
ClassContainingSomeParams();
void runSim();
int getP1();
int getP2();
private:
int p1, p2;
};
#endif
ClassContainingSomeParams.cpp
#include "ClassContainingSomeParams.hpp"
ClassContainingSomeParams::ClassContainingSomeParams() : p1(0), p2(0)
{}
void ClassContainingSomeParams::runSim()
{
SimEnv env(p1, p2);
// Do some stuff
}
main.cpp
#include "ClassContainingSomeParams.hpp"
#include "SimEnv.hpp"
int main()
{
ClassContainingSomeParams p;
SimEnv env(p);
// Do some stuff
return 0;
}
The exact errors I'm getting are
SimEnv.hpp:10:33: error: field 'ClassContainingParams' has incomplete type 'SimEnv'
ClassContainingSomeParams.hpp:5:1: error: 'ClassContainingSomeParams' does not name a type
Do I need to place the class declaration and initialisation into the header file? If so, why?
There is a circular inclusion between ClassContainingParams.hpp and SimEnv.hpp.
change your ClassContainingParams to ClassContainingParams* or const ClassContainingParams& and move the inclusion of ClassContainingParams.hpp in SimEnv.cpp.
#ifndef SIMENV
#define SIMENV
class ClassContainingSomeParams;
namespace Namespace
{
class SimEnv
{
private:
size_t p1, p2;
size_t* pTracker;
void init();
public:
SimEnv(const ClassContainingParams&);
SimEnv(size_t, size_t);
size_t func1();
size_t func2();
};
}
#endif
SimEnv.cpp
#include <SimEnv.hpp>
#include <ClassContainingSomeParams.hpp>
namespace Namespace
{
SimEnv::SimEnv(const ClassContainingSomeParams& p) :
p1(p.getP1()), p2(p.getP2())
{
init();
}
SimEnv(SimEnv(size_t p1, size_t p2) : p1(p1), p2(p2)
{
init();
}
void SimEnv::init()
{
std::cout << "I'm initialised" << '\n';
}
}

C++, Why can't I put the definition of class constructor with parameters-initialize list outside the class declaration [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Why can't I put the definition of class constructor with parameters-initialize list outside the class declaration?
typedef unsigned int UINT;
class num_sequence {
public:
typedef vector<UINT>::iterator iterator;
//I cannot put this following part in the cpp file
num_sequence(vector<UINT> & ele,int len=0,int beg=0):_relems(ele),_length(len),_beg_pos(beg)
{
//an abstract class cannot be instanlized
cout<<"build a num_sequence object";
}
virtual ~num_sequence();
num_sequence.h
#include <vector>
typedef unsigned int UINT;
class num_sequence
{
public:
typedef std::vector<UINT>::iterator iterator;
num_sequence(std::vector<UINT> & ele, int len = 0, int beg = 0);
virtual ~num_sequence();
private:
std::vector<UINT> &_relems;
int _length;
int _beg_pos;
};
num_sequence.cpp
#include "num_sequence.h"
#include <iostream>
num_sequence::num_sequence(std::vector<UINT> & ele, int len, int beg)
: _relems(ele), _length(len), _beg_pos(beg)
{
std::cout << "build a num_sequence object";
}
num_sequence::~num_sequence()
{
}

Create a variable inside another statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to allocate a variable within the scope of a parameter list? By using new we can do the following :
Class A{ /*... snip ...*/ };
void myFunc(A* a){ }
int main(...){
myFunc(new A());
return 0;
}
This will create a new A. What if the signature of myFunc was
void myFunc(A a);
instead. Is there a syntax to create local instance inside the myFunc() parameter list? I'm looking for something like
myFunc(A());
or
myFunc(A a());
Another use would be for something like :
A a(123);
if(a == A(123)){ }
The net effect is to save one line, but it also creates a scope within the parameters list which makes me wonder if it is allowed at all.
If you just want to create a variable to pass to the function you can use a aggregate initialization / list initialization
#include <iostream>
#include <cmath>
using namespace std;
class A{ /*... snip ...*/ };
void myFunc(A a){ }
int main(){
myFunc(A{});
return 0;
}
Live Example
You can also use this with classes that have constructors that take multiple parameters
#include <iostream>
#include <cmath>
using namespace std;
class A
{
private:
int foo;
int bar;
double foobar;
public:
A(int a, int b, double c) : foo(a), bar(b), foobar(c) {}
};
void myFunc(A a){ }
int main(){
myFunc(A{1,2,3.0});
return 0;
}
Live Example
C++ supports this with the myFunc(A()); syntax you posed in your question.
#include <stdio.h>
char lazybuff[500];
class Point
{
public:
Point (double x, double y) : m_x(x), m_y(y) { }
char * ToString (void) { sprintf (lazybuff, "%f, %f", m_x, m_y); return lazybuff; }
private:
double m_x, m_y;
};
void print_point (Point print_me)
{
printf ("%s\n", print_me.ToString());
}
int main (void)
{
print_point (Point(5, 3));
return 0;
}

recursive_wrapper and ambiguous convert_construct() [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Do you have any idea how to solve or get around this problem?
http://boost.2283326.n4.nabble.com/Variant-recursive-wrapper-and-ambiguous-convert-construct-td4543139.html
#include <boost/variant.hpp>
class b_fwd;
typedef boost::variant<boost::recursive_wrapper<b_fwd> > a;
class c_fwd;
typedef boost::variant<boost::recursive_wrapper<c_fwd> > b;
struct b_fwd : b {
// inherit constructors
template <typename... Ts> b_fwd(Ts... o) : b(o...) {}
};
typedef boost::variant<double, char *> c;
struct c_fwd : c {
// inherit constructors
template <typename... Ts> c_fwd(Ts... o) : c(o...) {}
};
void f(const b &b_node)
{
a a_node(b_node);
}
int main()
{
}
I had a hunch in reaction to GuyGreer's comment.
If the problem is caused by variant being assignable/convertible to variant, then maybe we can disambiguate by using a third, isolated, variant:
a a_node(boost::variant<b>(b_node));
Lo and behold, homeopathy seems to work :)
See it Live On Coliru
I have no idea whether this "works" in the sense that the OP expects, because, frankly, the OP's code was a mystery to me
#include <boost/variant.hpp>
class b_fwd;
typedef boost::variant<boost::recursive_wrapper<b_fwd> > a;
class c_fwd;
typedef boost::variant<boost::recursive_wrapper<c_fwd> > b;
struct b_fwd : b {
using b::b;
};
typedef boost::variant<double, char *> c;
struct c_fwd : c {
using c::c;
};
void f(const b &b_node)
{
a a_node(boost::variant<b>(b_node));
}
int main()
{
}

codeblocks c++ many errors while making a class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I'm currently making my first project in codeblocks, but when i generate a new class it pops up a bunch of errors.
Code:
#ifndef SERVICIO_H
#define SERVICIO_H
#include <iostream>
#include <string>
using namespace std;
class Servicio
{
public:
Servicio();
virtual ~Servicio();
int codigo Get[10]() { return [10]; }
void Set[10](int codigo val) { [10] = val; }
string nombre Get[10]() { return [10]; }
void Set[10](string nombre val) { [10] = val; }
float precio Get[10]() { return [10]; }
void Set[10](float precio val) { [10] = val; }
float comision Get[10]() { return [10]; }
void Set[10](float comision val) { [10] = val; }
protected:
private:
int codigo [10];
string nombre [10];
float precio [10];
float comision [10];
}
#endif // SERVICIO_H
And the error log:
|12|error: expected ';' at end of member declaration|
|12|error: 'Get' does not name a type|
|13|error: expected ',' or '...' before 'val'|
|13|error: declaration of 'Set' as array of functions|
|13|error: expected ';' at end of member declaration|
|14|error: expected ';' at end of member declaration|
|14|error: 'Get' does not name a type|
|15|error: expected ',' or '...' before 'val'|
You need a ; after the closing bracket of the class.
If you can use C++11, then consider using std::array. See this for details.
#include <array>
#include <iostream>
class Servicio
{
public:
Servicio() { }
virtual ~Servicio() { }
We don't want to return by reference since you only want to get the value.
std::array<int, 10> get_codigo() const {
return codigo;
}
Here you can consider doing something with value before assigning it to codigo.
void set_codigo(const std::array<int, 10>& value) {
codigo = value;
}
protected:
private:
std::array<int, 10> codigo;
std::array<std::string, 10> nombre;
std::array<float, 10> precio;
std::array<float, 10> comision;
};
Either way this style of coding is cumbersome and probably not the correct approach.
What? This code is nothing like C++. You really need to read a book before you start coding. C++ is very different from whatever language you knew before. It's not just syntactically different, the concepts are different. You are not going to be able to code C++ just by using what you know already, you are going to have to do some studying.
I guess you're not likely to take the advice above, so here's start, it is at least legal code (but not good code though).
class Servicio
{
public:
Servicio();
int* GetCodigo() { return codigo; }
...
private:
int codigo [10];
};