C++ Standard library stack usage. Trouble pushing float array - c++

I am having trouble pushing an array to the stack. I thought this was pretty straightforward, but I've already spent too much time trying to figure this one out.
I expected to be able to push arrays just as I push ints or floats, but that is not happening.
The push command is giving me the issue. Here is my code:
#include <iostream>
#include <stack>
struct Matrix4x4
{
float data[16];
};
int main(int argc, char **argv)
{
// My original code
typedef std::stack<float[16]> myStack;
myStack modelViewStack;
myStack projectionStack;
float testMat[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
modelViewStack.push(testMat); // THIS LINE GIVES ME ERRORS
////Stack initialization - This is thokra's solution
////typedef std::stack<std::vector<float[16]>> myStack;
//typedef std::stack<Matrix4x4> myStack;
//myStack modelViewStack;
//myStack projectionStack;
//
//Matrix4x4 m = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
////std::vector<float> testMat2(testMat, testMat + sizeof(testMat) / sizeof(float));
//modelViewStack.push(m);
//for(int i = 0; i<16 ; i++)
//{
// std::cout << "m data: " << m.data[i] << std::endl;
//}
//system("pause");
return 0;
}
Thanks for your help!
Here are the errors. I can't decipher them. Maybe an explanation of how to read these would be helpful too.
1>------ Build started: Project: opengl4_4, Configuration: Release Win32 ------
1> main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(606): error C2075: 'Target of operator new()' : array initialization needs curly braces
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty (*),const _Ty (&))'
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty (*),const _Ty (&))' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\deque(925) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::allocator<float [16]>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\stack(21) : see reference to class template instantiation 'std::deque<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> main.cpp(14) : see reference to class template instantiation 'std::stack<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(606): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Just encapsulate the data store holding the elements of the matrix in a suitable type:
#include <stack>
struct Matrix4x4
{
float data[16];
};
int main()
{
typedef std::stack<Matrix4x4> myStack;
myStack modelViewStack;
Matrix4x4 m = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
modelViewStack.push(m);
return 0;
}
More to the point: std::stack::push will internally call push_back on the std::deque that's used as the container when you don't change the default. Essentially, when trying to construct a new element at the end of the deque the container tries to place the new element at the address which currently marks the end of the container with a placement-new. For instance, g++ implements it as follows:
template<typename _Up, typename... _Args>
void
construct(_Up* __p, _Args&&... __args)
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
This effectively boils down to:
::new((void *)__p) float[16](_args); // where __p is a pointer to float[16] and _args is testMat
Trying to copy- or move-initialize a C-array is simply not legal. Even if construction succeeded for some reason, the container would try to call the destructor on an element of type float[16] when going out of scope. One can easily see, a destructor ~T[n] does not exist.
In C++11, you can push a std::array<float,16> instead of defining an additional type.

Related

C++ Boost matrix error when transforming input to classes

First of all, I use boost library, and if it changes anything, the code is compiled on a Windows Machine.
The code itself contains a lot more of function acting upon matrices but only this one triggers the error.
Well, I am trying to transform matrix like :
{001
100
010}
To something like :
{1
3
2}
But strangely I can't compile my code and I can't find the error so I would be glad if anyone could help me.
Below the code :
using namespace boost::numeric::ublas;
typedef matrix <float, row_major, unbounded_array<float>> MATRIXf;
MATRIXf matrix_to_class (const MATRIXf inputM)
{
MATRIXf output;
for (std::size_t line = 0; line < inputM.size1(); line++)
{
for (std::size_t column = 0; column < inputM.size2(); column++)
{
if (column == 1)
{
output.insert_element(line,0.0,column);
}
}
}
return output;
}
Here is the error code:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): error C4996: 'std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): note: see declaration of 'std::copy::_Unchecked_iterators::_Deprecate'
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(204): note: see reference to function template instantiation '_OutIt *std::copy<float*,float*>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=float *,
1> _InIt=float *
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(201): note: while compiling class template member function 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>> &boost::numeric::ublas::unbounded_array<T,std::allocator<T>>::operator =(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)'
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(310): note: see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>> &boost::numeric::ublas::unbounded_array<T,std::allocator<T>>::operator =(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)' being compiled
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(102): note: see reference to class template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>' being compiled
1> with
1> [
1> T=float
1> ]
1> g:\c++ python\travail\visualstudio\visualstudio\guigui\neural net\neural net\utils.hpp(21): note: see reference to class template instantiation 'boost::numeric::ublas::matrix<float,boost::numeric::ublas::row_major,boost::numeric::ublas::unbounded_array<float,std::allocator<T>>>' being compiled
1> with
1> [
1> T=float
1> ]
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory(102): error C4996: 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate': Call to 'std::uninitialized_copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory(102): note: see declaration of 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate'
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(94): note: see reference to function template instantiation '_FwdIt *std::uninitialized_copy<const float*,float*>(_InIt,_InIt,_FwdIt)' being compiled
1> with
1> [
1> _FwdIt=float *,
1> _InIt=const float *
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(89): note: while compiling class template member function 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>::unbounded_array(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)'
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(162): note: see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>::unbounded_array(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)' being compiled
1> with
1> [
1> T=float
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Trying to locate the error brings me to the function above.
Thanks in advance.
Near the end of the first error message is the text, "To disable this warning, use -D_SCL_SECURE_NO_WARNINGS". https://msdn.microsoft.com/en-us/library/ttcz0bys.aspx has a more detailed discussion of warnings and checked iterators. In essence, Microsoft created "safe" mutations of Standard C++ functions to help developers avoid invalid iterator usage. The error message suggests that you define _SCL_SECURE_NO_WARNINGS. This can be done in the project properties C/C++/Preprocessor/Preprocessor Definitions. In a project I worked on in the past, we disabled all the "safe" versions of the functions because of the performance hit.
You may be interested in reading the above Microsoft page for more information about the checked iterator topic.

Compilation issues in C++ while, trying to create a std:: thread by calling a member function in another object

WRT below code, I'm finding compilation issues while, trying to create a thread by calling a member function in another object.
th = std::thread(&AbcSFriend::S2F,this,friendObj);
is the culprit line causing below compilation error. If i remove this line iit compiles fine.
class AbcSFriend
{
public:
void S2F(Abc* ptr)
{}
};
class Abc
{
public:
std::thread th;
AbcSFriend frinedObj;
void FL()
{
th = std::thread(&AbcSFriend::S2F,this,friendObj);
}
};
Cannot generate copy-ctor or copy-assignment operator when UDT
contains a zero-sized array 1>C:\Program Files (x86)\Microsoft Visual
Studio 12.0\VC\include\functional(1149): error C2664: 'eUserErrorCode
std::_Pmf_wrap::operator ()(_Farg0 &,Abc *) const' : cannot convert argument 2 from 'AbcSFriend' to 'Abc *' 1>
with 1> [ 1> _Farg0=AbcSFriend 1> ] 1>
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called 1> C:\Program
Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional(1137) :
see reference to function template instantiation '_UserErrorCode
std::_Bind,Abc
*,AbcSFriend>::_Do_call<,0x00,0x01>(std::tuple<>,std::_Arg_idx<0x00,0x01>)'
being compiled 1> C:\Program Files (x86)\Microsoft Visual
Studio 12.0\VC\include\functional(1137) : see reference to function
template instantiation '_UserErrorCode
std::_Bind,Abc
*,AbcSFriend>::_Do_call<,0x00,0x01>(std::tuple<>,std::_Arg_idx<0x00,0x01>)'
being compiled 1> C:\Program Files (x86)\Microsoft Visual
Studio 12.0\VC\include\thr/xthread(195) : see reference to function
template instantiation '_UserErrorCode
std::_Bind,Abc
*,AbcSFriend>::operator ()<>(void)' being compiled 1> C:\Program Files (x86)\Microsoft Visual Studio
12.0\VC\include\thr/xthread(195) : see reference to function template instantiation '_UserErrorCode
std::_Bind,Abc
*,AbcSFriend>::operator ()<>(void)' being compiled 1> C:\Program Files (x86)\Microsoft Visual Studio
12.0\VC\include\thr/xthread(192) : while compiling class template member function 'unsigned int
std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)' 1>
with 1> [ 1>
_Target=std::_Bind,Abc
*,AbcSFriend> 1> ] 1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thr/xthread(187) : see
reference to function template instantiation 'unsigned int
std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)' being
compiled 1> with 1> [ 1>
_Target=std::_Bind,Abc
*,AbcSFriend> 1> ] 1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thr/xthread(205) : see
reference to class template instantiation 'std::_LaunchPad<_Target>'
being compiled 1> with 1> [ 1>
_Target=std::_Bind,Abc
*,AbcSFriend> 1> ] 1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thread(49) : see
reference to function template instantiation 'void
std::_Launch,Abc
*,AbcSFriend>>(_Thrd_t *,_Target &&)' being compiled 1> with 1> [ 1>
_Target=std::_Bind,Abc
*,AbcSFriend> 1> ] 1> ....\Sources\SOMEPLACESource.cpp(254) : see reference to function
template instantiation 'std::thread::thread(_Fn &&,Abc const
&&,AbcSFriend &)' being compiled 1> with 1> [ 1>
_Fn=eUserErrorCode (__cdecl AbcSFriend:: )(Abc *)
frinedObj (in AbcSFriend frinedObj;)
is not
friendObj (in th = std::thread(&AbcSFriend::S2F,this,friendObj);).
Fix the spelling.
As 'Maxim Egorushkin' says, try:
class AbcSFriend
{
public:
void S2F(class Abc* ptr);
};
class Abc
{
public:
std::thread th;
AbcSFriend friendObj;
void FL()
{
th = std::thread(&AbcSFriend::S2F, &friendObj, this);
}
};
I think the correct order of arguments is:
th = std::thread(&AbcSFriend::S2F, &friendObj, this);
It also makes sense to reverse the order of the declaration of members th and friendObj because currently friendObj destroys before th leaving open a possibility of th using friendObj after friendObj destroyed. Declare friendObj first and th last.

Compiler error C2664 and xmemory0

I'm trying to pinpoint where the location of this error is coming from. C2664 is a parameter conversion problem that isn't showing up as an error in my coding, but rather something that is written in xmemory0 (or at least, that's how i'm interpreting it). I understand that part of what it's complaining about is that it is being asked to convert from an allocator to a string. I'm not using an allocator in this file.
The error reads:
C2664 'std::pair<const _Kty,_Ty>::pair(std::pair<const _Kty,_Ty>
&&)': cannot convert argument 1 from
'std::vector<std::string,std::allocator<_Ty>>' to 'const std::string &'
Project1 c:\program files (x86)\microsoft visual studio
14.0\vc\include\xmemory0, Line 737
The code in question:
/*
Interpreter class implementation: utilizes the lex scanner class as
well as the expression evaluator to interpret simple statements like
read display and assignment statements
!!: indicates current problems or things that are going to be problems.
*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include "lexScanner.h"
#include "expEvaluator.h"
#include "interpreter.h"
using namespace std;
void Interpreter::executeProgram(vector<string>& sourceProgram)
{
// ****************************************************
// PARSING THE INFORMATION:
// Intialize all containers for storage of broken down string components
// Resize al appropriate vectors to the size of the source program
// ****************************************************
// !!: This probably wont work. Source program is going to be magnitudes
// smaller than the amount of category tokens
// but it worked in the last programming project soooo. .. .
vectOfTokenVects myVectOfTokenVects;
vectOfCategoryVects myVectOfCategoryVects;
perLineCategoryVector myPerLineCatVect;
// Send for parsing
LexicalScanner::getLexicalInfo(sourceProgram,myVectOfTokenVects,myVectOfCategoryVects);
//Display the lexical information for user's means
cout << "Here is a preview of the source code to be executed. . \n";
// Create a string-to-float map that serves as a symbol table to maintain
// the names of variables and their values during execution
// !!: figure out how to store items in the map
floatVarValueTable varTable;
// ****************************************************
// EXECUTING A READ STATEMENT:
// Obtain numerical input from the user and
// store it properly in the corresponding variable in the map as a symbol
// table.
// ****************************************************
// Cycle through the tokens, searching for the three categories
// If the catToken is keyword AND IS read, go ahead and execute a cin
// If the catToken is keyword AND IS display, go ahead and execute a cout
// If the catToken is an assignment statement, figure out how far the
// math expression goes. Try to compute the line. Search comma to comma?
// ****************************************************
for (int i = 0; i < myVectOfCategoryVects.size(); i++)
{
// Start out with the largest net: search for keywords
for (int j = 0; j < myVectOfCategoryVects[i].size(); j++)
{
if (myVectOfCategoryVects[i][j] == KEYWORD)
{
// SCENARIO 1: READ STATEMENT
// Have the user enter the value for the variable to be added to vartable.
if ((myVectOfTokenVects[i][j] == "Read") || (myVectOfTokenVects[i][j] == "READ") || (myVectOfTokenVects[i][j] == "read"))
{
float reportedValue;
cout << "Please enter a value for: " << myVectOfTokenVects[i][j + 1] << endl;
cin >> reportedValue;
string dumpedString = myVectOfTokenVects[i][j + 1];
varTable.emplace(dumpedString, reportedValue);
}
// SCENARIO 2: DISPLAY STATEMENT
// Search the vector (token vector) for things to display.
if (myVectOfTokenVects[i][j] == "Display" || myVectOfTokenVects[i][j] == "display" || myVectOfTokenVects[i][j] == "DISPLAY")
{
// Search the vector continiously until you reach a comma or semicolon. Both comma and semicolon indicate the end of the display statement.
int currentPosition = j+1;
for (int j = currentPosition; j > myVectOfTokenVects[i].size(); i++)
{
if (myVectOfCategoryVects[i][j] != COMMA)
{
cout << myVectOfTokenVects[i][j];
}
// We've got something after the comma, prob an expression. Send it to expression evaluator
if (myVectOfCategoryVects[i][j] == COMMA)
{
expVector infixExpression;
float expValue;
// Start filling up the expression vector
for (int k = j; k > myVectOfTokenVects[i][j].size(); k++)
{
infixExpression.push_back(myVectOfTokenVects[i][k]);
}
// Take the newly formed expression vector and send it to the evaluator. It does pass by reference, so the values in the vartable and infixexpression are going to be updated
ExpressionEvaluator::infixEvaluator(infixExpression, varTable, expValue);
cout << expValue;
}
}
}
}
// ****************************************************
// SEARCHING FOR VARIABLES
// If the token in question is an assignment operator, go ahead
// and insert the item before and after it.
// Note: This leaves a hole stating that if there is a floating
// equals sign somewhere with nothing before or after it, it
// could cause a out of bounds error.
// Note: We should also check for the presence of an equation.
// Evaluate the RHS of the assignment op
if (myVectOfCategoryVects[i][j] == ASSIGNMENT_OP)
{
// SCENARIO 1: SIMPLE EVALUATION
// If it's a simple 3 part assignment statement: ie:
// name (=) value, go ahead and store it
if (myVectOfCategoryVects[i][j + 2])
{
// Make sure the assignment op has a var name
if (myVectOfCategoryVects[i][j - 1])
{
// Store both the name and value
varTable.emplace(myVectOfTokenVects[j - 1], myVectOfTokenVects[j + 1]);
}
}
}
// SCENARIO 2: COMPLEX EVALUATION
// More complex evaluation: sending the RHS to the evaluator
// NOTE: postfix evaluator needs to be fixed. .
if (myVectOfCategoryVects[i][j] == ASSIGNMENT_OP)
{
// Semicolon out of normal place, assume incoming expression
if (myVectOfCategoryVects[i][j + 2] != SEMICOLON)
{
// NOTE: The placement of this is intentional. . .it guarentees that they get cleared on OOS
expVector infixExpression;
float expValue;
// Start with whatever is before the assignment op, start adding items to the infixExpression vector. Lookout for semicolon
for (int j = 0; myVectOfCategoryVects[i][j] != SEMICOLON; i++)
{
infixExpression.push_back(myVectOfTokenVects[i][j]);
}
// Take the newly formed expression vector and send it to the evaluator. It does pass by reference, so the values in the vartable and infixexpression are going to be updated
ExpressionEvaluator::infixEvaluator(infixExpression, varTable, expValue);
}
}
}
}
}
Relative definitions:
//****************************************************************************
// define a mnemonic name for the type to store tokens of one line of statement
//****************************************************************************
typedef vector<string> perLineTokenVector;
//****************************************************************************
// define a mnemonic name for the type to store tokens of lines of statement
//****************************************************************************
typedef vector<perLineTokenVector> vectOfTokenVects;
//****************************************************************************
// define a mnemonic name for the type to store categories of tokens
// of one line of statement
//****************************************************************************
typedef vector<tokenCategory> perLineCategoryVector;
//****************************************************************************
// define a mnemonic name for the type to store categories of tokens
// of one line of statement
//****************************************************************************
typedef vector<perLineCategoryVector> vectOfCategoryVects;
edit: The error seems to reside in this area
if (myVectOfCategoryVects[i][j] == ASSIGNMENT_OP)
{
// SCENARIO 1: SIMPLE EVALUATION
// If it's a simple 3 part assignment statement: ie:
// name (=) value, go ahead and store it
if (myVectOfCategoryVects[i][j + 2]=SEMICOLON)
{
// Make sure the assignment op has a var name
if (myVectOfCategoryVects[i][j - 1] = STRING_LITERAL)
{
string stringDump = myVectOfTokenVects[i][j - 1];
// Store both the name and value
varTable.emplace(stringDump, myVectOfTokenVects[i][j + 1]);
}
}
}
Build output
1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1> interpreter.cpp
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(67): warning C4018: '<': signed/unsigned mismatch
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(70): warning C4018: '<': signed/unsigned mismatch
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(94): warning C4018: '>': signed/unsigned mismatch
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(106): warning C4018: '>': signed/unsigned mismatch
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): error C2664: 'std::pair<const _Kty,_Ty>::pair(std::pair<const _Kty,_Ty> &&)': cannot convert argument 2 from 'std::string' to 'const float &'
1> with
1> [
1> _Kty=std::string,
1> _Ty=float
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): note: Reason: cannot convert from 'std::string' to 'const float'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,std::string&,std::string&>(_Objty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Objty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,std::string&,std::string&>(_Objty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Objty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(996): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::string&,std::string&>(std::allocator<_Other> &,_Objty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Tree_node<std::pair<const std::string,float>,void *>>,
1> _Ty=std::pair<const std::string,float>,
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Objty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(995): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::string&,std::string&>(std::allocator<_Other> &,_Objty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Tree_node<std::pair<const std::string,float>,void *>>,
1> _Ty=std::pair<const std::string,float>,
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Objty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(889): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,std::string&,std::string&>(_Ty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Ty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(887): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,std::string&,std::string&>(_Ty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Ty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(1076): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_comp_alloc<_Traits>::_Buynode<std::string&,std::string&>(std::string &,std::string &)' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=float,
1> _Traits=std::_Tmap_traits<std::string,float,std::less<std::string>,std::allocator<std::pair<const std::string,float>>,false>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(1076): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_comp_alloc<_Traits>::_Buynode<std::string&,std::string&>(std::string &,std::string &)' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=float,
1> _Traits=std::_Tmap_traits<std::string,float,std::less<std::string>,std::allocator<std::pair<const std::string,float>>,false>
1> ]
1> c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(145): note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::string&,std::basic_string<char,std::char_traits<char>,std::allocator<char>>&>(std::string &,std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=float,
1> _Pr=std::less<std::string>,
1> _Alloc=std::allocator<std::pair<const std::string,float>>
1> ]
1> c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(145): note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::string&,std::basic_string<char,std::char_traits<char>,std::allocator<char>>&>(std::string &,std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=float,
1> _Pr=std::less<std::string>,
1> _Alloc=std::allocator<std::pair<const std::string,float>>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
edit 3
It appears that the problem was with what I was passing to the emplacement function for the map. myVectOfTokenVects is a fancy string vector of string vectors. The second argument to the map.emplacement() requires a float value, of which myVectOfTokenVects cannot supply. The problem has been found.
Follow-up:
How would I go about parsing the string value to cast it to a float for map.emplacement()?

CUDA thrust::device_vector of class | error

I am new to CUDA and the thrust library. I've been looking through a lot of examples and questions regarding my problem, however, I was not able to transfer a solution.
I have a class Cell which should contain a vector of Tree (another class).
This is my Cell.h
#pragma once
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/reduce.h>
#include <thrust/functional.h>
#include "Tree.h"
class Cell {
int idx;
float xmid, ymid;
float dx, dy;
int nTrees;
thrust::host_vector<Tree> trees;
// thrust::device_vector<Tree> trees; <-- this is what I want
public:
Cell();
Cell(int, float, float, float, float, int);
void set(int, float, float, float, float, int);
void add(float, float, float);
void add(float);
void add();
virtual ~Cell();
void print();
void copyToDev();
};
and here is my Tree.h
#pragma once
#include <iostream>
#include <cstdlib>
using namespace std;
class Tree {
float x, y, r;
int idx;
public:
Tree();
Tree(float, float, float, int);
void set(float, float, float, int);
virtual ~Tree();
void print();
};
The classes are both implemented in a file with extension .cu. In my main.cu I now want to initialize a Cell C. Given the code above, I am able to compile the code (using Visual Studio 2013 which I haven't used before, so that might be another problem for me).
However, if I'm not mistaken, using host_vector I do not make use of my GPU. What I want is to use device_vector instead.
But if I compile the code with device_vector instead of host_vector I get the following error
1>------ Build started: Project: WTM, Configuration: Debug Win32 ------
1> Cell.cu
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\detail\error_category.inl(102): warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h(168) : see declaration of 'strerror'
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\cuda\detail\bulk\detail\pointer_traits.hpp(55): warning C4800: 'unsigned int' : forcing value to bool 'true' or 'false' (performance warning)
1>c:\users\thomas\documents\visual studio 2013\projects\wtm\wtm\cell.cu(40): warning C4018: '<' : signed/unsigned mismatch
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\cuda\detail\for_each.inl(84): error C2027: use of undefined type 'thrust::detail::STATIC_ASSERTION_FAILURE<false>'
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\for_each.inl(71) : see reference to function template instantiation 'RandomAccessIterator thrust::system::cuda::detail::for_each_n<thrust::system::cuda::detail::tag,InputIterator,Size,UnaryFunction>(thrust::system::cuda::detail::execution_policy<thrust::system::cuda::detail::tag> &,RandomAccessIterator,Size,UnaryFunction)' being compiled
1> with
1> [
1> RandomAccessIterator=thrust::device_ptr<Tree>
1> , InputIterator=thrust::device_ptr<Tree>
1> , Size=int
1> , UnaryFunction=thrust::detail::allocator_traits_detail::gozer
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\allocator\destroy_range.inl(136) : see reference to function template instantiation 'InputIterator thrust::for_each_n<DerivedPolicy,Pointer,Size,thrust::detail::allocator_traits_detail::gozer>(const thrust::detail::execution_policy_base<DerivedPolicy> &,InputIterator,Size,UnaryFunction)' being compiled
1> with
1> [
1> InputIterator=thrust::device_ptr<Tree>
1> , DerivedPolicy=thrust::system::cuda::detail::tag
1> , Pointer=thrust::device_ptr<Tree>
1> , Size=int
1> , UnaryFunction=thrust::detail::allocator_traits_detail::gozer
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\allocator\destroy_range.inl(157) : see reference to function template instantiation 'void thrust::detail::allocator_traits_detail::destroy_range<Allocator,Pointer,Size>(Allocator &,Pointer,Size)' being compiled
1> with
1> [
1> Allocator=thrust::device_malloc_allocator<Tree>
1> , Pointer=thrust::device_ptr<Tree>
1> , Size=int
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\contiguous_storage.inl(256) : see reference to function template instantiation 'void thrust::detail::destroy_range<thrust::device_malloc_allocator<T>,Base,int>(Allocator &,Pointer,Size)' being compiled
1> with
1> [
1> T=Tree
1> , Base=thrust::device_ptr<Tree>
1> , Allocator=thrust::device_malloc_allocator<Tree>
1> , Pointer=thrust::device_ptr<Tree>
1> , Size=int
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\contiguous_storage.inl(255) : while compiling class template member function 'void thrust::detail::contiguous_storage<T,Alloc>::destroy(thrust::detail::normal_iterator<thrust::device_ptr<T>>,thrust::detail::normal_iterator<thrust::device_ptr<T>>)'
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\vector_base.inl(474) : see reference to function template instantiation 'void thrust::detail::contiguous_storage<T,Alloc>::destroy(thrust::detail::normal_iterator<thrust::device_ptr<T>>,thrust::detail::normal_iterator<thrust::device_ptr<T>>)' being compiled
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\vector_base.inl(44) : while compiling class template member function 'thrust::detail::vector_base<T,Alloc>::vector_base(void)'
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\device_vector.h(67) : see reference to function template instantiation 'thrust::detail::vector_base<T,Alloc>::vector_base(void)' being compiled
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\device_vector.h(54) : see reference to class template instantiation 'thrust::detail::vector_base<T,Alloc>' being compiled
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\users\thomas\documents\visual studio 2013\projects\wtm\wtm\cell.h(27) : see reference to class template instantiation 'thrust::device_vector<Tree,thrust::device_malloc_allocator<T>>' being compiled
1> with
1> [
1> T=Tree
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'm really not sure how to approach this. Maybe someone could guide me through this problem. Thank you in advance!
EDIT
As pointed out, I forgot the main, this is the code below, aso in a .cu file
#include "Cell.h"
int main(void)
{
Cell D(0, 0.5, 0.5, 1, 1);
D.print();
return 0;
}
reduced to the minimal part. If further information are missing, just let me know.
If you look into the file with the definition of thrust::system::cuda::detail::for_each_n like the error message is suggesting, you will find the following comment (link to github):
// we're attempting to launch a kernel, assert we're compiling with nvcc
// ========================================================================
// X Note to the user: If you've found this line due to a compiler error, X
// X you need to compile your code using nvcc, rather than g++ or cl.exe X
// ========================================================================
Make sure you are really using nvcc to compile your program, and not merely trying to include thrust in a normal c++ project which gets compiled with visual studio's cl.

error C2582: 'operator =' function is unavailable. (xutility)

I'm getting an error occurring in the 'xutility' class - It is locked as I did not create it
error C2582: 'operator =' function is unavailable in 'Agent'
The error points to these lines in the code:
// TEMPLATE FUNCTION move
template<class _InIt,
class _OutIt> inline
_OutIt _Move(_InIt _First, _InIt _Last,
_OutIt _Dest, _Nonscalar_ptr_iterator_tag)
{ // move [_First, _Last) to [_Dest, ...), arbitrary iterators
for (; _First != _Last; ++_Dest, ++_First)
*_Dest = _STD move(*_First); // this line has the error
return (_Dest);
}
Why is this occurring? What does it mean and how can I fix it?
EDIT - this is what I grabbed from the output, can someone help me understand this? Sorry to be a complete newbie...
1>------ Build started: Project: D3D10DEMO, Configuration: Debug Win32 ------
1> Level.cpp
1>c:\users\asher\documents\my dropbox\direct3d\d3d10demo_1.0\d3d10demo\level.cpp(449): warning C4018: '<' : signed/unsigned mismatch
1> Brain.cpp
1>c:\users\asher\documents\my dropbox\direct3d\d3d10demo_1.0\d3d10demo\brain.cpp(43): warning C4413: 'Brain::nodes' : reference member is initialized to a temporary that doesn't persist after the constructor exits
1> c:\users\asher\documents\my dropbox\direct3d\d3d10demo_1.0\d3d10demo\brain.h(34) : see declaration of 'Brain::nodes'
1>c:\users\asher\documents\my dropbox\direct3d\d3d10demo_1.0\d3d10demo\brain.cpp(43): warning C4413: 'Brain::roomNodeVectors' : reference member is initialized to a temporary that doesn't persist after the constructor exits
1> c:\users\asher\documents\my dropbox\direct3d\d3d10demo_1.0\d3d10demo\brain.h(35) : see declaration of 'Brain::roomNodeVectors'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2514): error C2582: 'operator =' function is unavailable in 'Agent'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2535) : see reference to function template instantiation '_OutIt std::_Move<_InIt,_OutIt> (_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)' being compiled
1> with
1> [
1> _OutIt=Agent *,
1> _InIt=Agent *
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1170) : see reference to function template instantiation '_OutIt std::_Move<Agent*,Agent*>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=Agent *,
1> _InIt=Agent *
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1165) : while compiling class template member function 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>)'
1> with
1> [
1> _Myvec=std::_Vector_val<Agent,std::allocator<Agent>>,
1> _Ty=Agent
1> ]
1> c:\users\asher\documents\my dropbox\direct3d\d3d10demo_1.0\d3d10demo\brain.h(41) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=Agent
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2514): error C2582: 'operator =' function is unavailable in 'Pickup'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2535) : see reference to function template instantiation '_OutIt std::_Move<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)' being compiled
1> with
1> [
1> _OutIt=Pickup *,
1> _InIt=Pickup *
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1170) : see reference to function template instantiation '_OutIt std::_Move<Pickup*,Pickup*>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=Pickup *,
1> _InIt=Pickup *
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1165) : while compiling class template member function 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>)'
1> with
1> [
1> _Myvec=std::_Vector_val<Pickup,std::allocator<Pickup>>,
1> _Ty=Pickup
1> ]
1> c:\users\asher\documents\my dropbox\direct3d\d3d10demo_1.0\d3d10demo\brain.h(44) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=Pickup
1> ]
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You seem to be calling a function template of the standard library with a type Agent that cannot be move-assigned to, yet the algorithm invoked needs to do just that.
As Als said in a comment to your question, you need to show the code that invokes this algorithm.
Of course you need to provide the agent class.
Assuming C++11, you need to implement
struct Agent
{
// .... other stuff
Agent(Agent&& other) { /* ... */ }
Agent& operator=(Agent&& other) { /* ... */ return *this; }
};
which is known as the Move Assignment operator. While you're at it, you might want to implement the move constructor as they go hand-in-hand:
Agent(Agent&& other) { /* ... */ }
Recommended reading:
http://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors
http://akrzemi1.wordpress.com/2011/08/11/move-constructor/
and many others