C++ 11 Delegate Constructor With New Instance Parameter? - c++

Having an issue getting this syntax to compile using the Visual Studio Nov 2012 CTP C++ Compiler ... Just wanted to be sure I wasn't missing something obvious.
Thanks!
EDIT: Removed Header to make it even simpler.
class Location
{
public:
Location();
};
class Shape
{
public:
Shape();
Shape(Location location);
};
// Doing this by pointer works ...
// Shape::Shape(Location* location){}
// Shape::Shape() : Shape(new Location()){}
Shape::Shape(Location location)
{
}
Shape::Shape()
: Shape(Location())
// error C2143: syntax error: missing ';' before ':'
{
// int x = 0;
// (void) x; // Added these two lines in some cases to get it to compile.
// These two lines do nothing, but get around a compiler issue.
}

// .h Simplification
class Location
{
public:
Location() {}
Location(Location const& other) {}
};
class Shape
{
Shape();
Shape(Location location);
};
// How about by value or reference?
Shape::Shape(Location location)
{
}
Shape::Shape(void)
: Shape(Location()) // error C1001: An internal error has occurred in the compiler.
{
}
int main() {}
The above code compiles and runs in gcc 4.7.2
I had to make a few changes to your code to make it compile. When simplifying things, try to keep the simplified code compiling. http://sscce.org/

Related

false-positive error using std::functional in vs2015

I have implemented some test functionals yesterday and everything compiled and worked fine without errors. Today i came back to my PC and my std::bind's are underlined red but compile without error. Seems like Intellisense and the compiler do not agree on the std::bind type. How can I fix this?
#include <functional>
class MyClass {
public:
int doE() {
return 0;
}
int doF() {
return 1;
}
};
void main()
{
MyClass obj;
std::function<int()> f = std::bind(&MyClass::doE, obj); // underlined red
std::cout << f();
}
The error message is as follows:
Error (active)
no suitable user-defined conversion from "std::_Binder<std::_Unforced, int (MyClass::*)(), MyClass &>" to "std::function<int ()>" exists
functionals
c:\functionals\functionals\thirdFunctionOnObject.h
I do have the same error type (Intellisense saying there is an error, but it compiles just fine) in more sophisticated code, where I used std::mem_fn().
Had the same problem with VS 2015 C++, I hate it. Microsoft drives me crazy.
For now I am using a nasty work around by moving the code to a static function. In your case it will be similar to the following:
class MyClass {
// other code
int doE () {
return 0;
}
static int statDoE(MyClass * myClass) {
return myClass->doE();
}
}

Template object as class parameter gives error before compiling

Here is the code:
#include <iostream>
using namespace std;
template<class OwnerType>
class Move {
public:
Move() {}
Move(OwnerType &_owner) {
owner = &_owner;
}
void GetPosition() {
cout << owner->x << endl;
}
OwnerType *owner;
};
class Entity {
public:
int x = 50;
Move<Entity> *move;
};
int main() {
Entity en;
en.x = 77;
en.move = new Move<Entity>(en); // sign '=' is underlined by VS
en.move->GetPosition();
return 0;
}
Error it gives :
a value of type "Move<Entity> *" cannot be assigned to an entity of type "Move<Entity> *"
The program is compiling, working as expected and gives expected values, however error is still here.
It's probably something to do with templates and compiling time and stuff but I don't have enough knowledge to know what this error actually represents.
Also don't worry about leaks since this was just me testing, error is what I don't understand.
Thanks in advance.
Intellisense is known for displaying invalid errors (see for example Visual Studio 2015: Intellisense errors but solution compiles), trust the compiler and linker, as suggested in comments.
However, this error is quite annoying, try closing the solution, delete the .suo file (it's hidden), and open in again. More info on what a .suo file is given here Solution User Options (.Suo) File
Side note, in your code example main is missing ().
so it is not Error.
it is Intellisense :
see:
Error 'a value of type "X *" cannot be assigned to an entity of type "X *"' when using typedef struct
Visual Studio 2015: Intellisense errors but solution compiles
old:
your main needs ():
this works for me:
#include<iostream>
using namespace std;
template<class T> class Move {
public:
Move() {}
Move(T &_owner) {
owner = &_owner;
}
void GetPosition() {
cout << owner->x << endl;
}
T *owner;
};
class Entity {
public:
int x = 50;
Move<Entity> *move;
};
int main(){
Entity en;
en.x = 77;
en.move = new Move<Entity>(en); // sign '=' is underlined by VS
en.move->GetPosition();
return 0;
}
output:
77

error: expected constructor, destructor, or type conversion before '&' token for a user defined class in the source file

I am facing an error in one of my projects which I have replicated using a standalone program. I did see several posts pertinent to this, but could not figure out my problem. I am getting the following error with this code : "error: expected constructor, destructor, or type conversion before '&' token"
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
class X
{
private:
int _x;
public:
X(int x) : _x(x) {};
};
class Y
{
private:
typedef boost::shared_ptr<X> X_ptr;
public:
X_ptr& func1();
};
X_ptr& Y::func1()
{
X_ptr p(new X(8));
return p;
};
int main()
{
return 0;
}
Can someon help me with in resolving this error?
There are two problems. First, you forgot to qualify the type name X_ptr:
Y::X_ptr& Y::func1()
// ^^^ ^
// BUT REMOVE THIS!
Second, notice that you are returning a reference to a local variable. Attempting to dereference the value returned by func1() will give you undefined behavior.
Just change the prototype of your function this way:
Y::X_ptr Y::func1()
// ^^^^^
// Returning the smart pointer by value now
{
X_ptr p(new X(8));
return p;
}

How can I fix my Factory Pattern to eradicate these compile errors?

My goal is to create a system wherein I can provide the string name of an class at run time and have it return an instance of that class in turn.
Searching stackoverflow, I came across an example that seems to do exactly what I am trying to accomplish, although I am currently unable to have it compile properly. The following is based on that code:
//LevelObject.h
#pragma once
#include <map>
#include <string>
class LevelObject
{
protected:
int ID;
public:
template<class T> static LevelObject* createT(void)
{
return new T(0);
}
LevelObject(void);
~LevelObject(void);
};
struct BaseFactory
{
typedef std::map<std::string, LevelObject*(*)()> map_type;
static LevelObject* createInstance(const std::string& s)
{
map_type::iterator it = getMap()->find(s);
if(it == getMap()->end())
{
return 0;
}
return it->second();
}
private:
static map_type* objectMap;
protected:
static map_type* getMap()
{
if(!objectMap)
{
objectMap= new map_type;
}
return objectMap;
}
};
template<class T>
struct DerivedRegister : BaseFactory
{
DerivedRegister(const std::string& s)
{
getMap()->insert(std::make_pair( s, &LevelObject::createT<T> ));
}
};
//Item.h
#pragma once
#include "LevelObject.h"
class Item :
public LevelObject
{
int ID;
static DerivedRegister<Item> reg;
public:
Item(int id);
~Item(void);
};
//Item.cpp
#include "Item.h"
Item::Item(int id)
{
ID = id;
}
Item::~Item(void)
{
}
DerivedRegister<Item> Item::reg("item");
The logic is that the derived objects, i.e. Item, will register a string and reference to a function that returns an instance of itself. On calling createInstance, it will take in a user inputted string and use the map to determine the object to return.
Unfortunately, this code is not compiling correctly, and gives me the following errors:
Error 1 error C2752:
'std::tr1::_Remove_reference<_Ty>' :
more than one partial specialization
matches the template argument list
Error 2 error C2528: 'abstract
declarator' : pointer to reference is
illegal c:\program files\microsoft
visual studio
10.0\vc\include\type_traits 965
Error 3 error C2528: 'type' : pointer
to reference is illegal c:\program
files\microsoft visual studio
10.0\vc\include\type_traits 349
If someone can help smooth out these errors, I would greatly appreciate it.
Or perhaps I am going about this entirely wrong in the first place, so if someone instead feels that I should be going in a different direction entirely please let me know.
Thanks in advance.
It's been a long time since this question was posted, but since there's no answer and I stumbled here too, I figured I'd add one. I copied the same factory code you did (from the StackOverflow answer here) and had the same problem. I found the solution at this StackOverflow answer.
It turns out Visual Studio 2010 (which I'm assuming you're using) has a problem with std::make_pair. Just use std::pair<std::string,LevelObject*(*)()> instead and you'll be good to go. At least that resolved this exact same problem for me.
I added empty bodies to the LevelObject class constructor and destructor:
LevelObject(void) { }
~LevelObject(void) { }
Then declared the static map member variable of the BaeFactory class:
BaseFactory::map_type* BaseFactory::map;
and the code compiled without errors in both GCC and Visual Studio.

BOOST_FOREACH: What is the error on using this of a STL container?

Does anyone know why the following generates an error on VC9?
class Elem;
class ElemVec : public vector<Elem>
{
public:
void foo();
};
void ElemVec::foo()
{
BOOST_FOREACH(Elem& elem, *this)
{
// Do something with elem
}
return;
}
The error I get is:
error C2355: 'this' : can only be referenced inside non-static member functions
The only (hack) solution I have right now which compiles without error is:
void ElemVec::foo()
{
ElemVec* This = this;
BOOST_FOREACH(Elem& elem, *This)
{
// Do something with elem
}
return;
}
You shouldn't inherit from STL containers. These are not polymorphic classes and it's the reason BOOST_FORACH can't handle your derived class.
Try to use aggregation instead.
Which compiler/Boost version are you using? I can compile the following without any problem (VS2005/Boost 1.38):
#include <boost/foreach.hpp>
using namespace std;
struct xxx : std::vector<int>
{
void test()
{
BOOST_FOREACH(int x, *this)
{
}
}
};
int main(void) {
xxx x;
x.test();
return 0;
}
Search the Boost bugbase if you want more details.
I had never seen that error. I guess it comes from the implementation of BOOST_FOREACH macro.
May i ask why you're creating a class based on vector<...> and not having a vector member variable ?
EDIT
Following this thread, i found out that this actually is a visual studio bug. The solution you have found seems to be the simplest.
Hmm, all compiled succesfully on my msvc (2005) compiller.
Maybe you have some error, but fixed or avoided it when was created your example.