I get these errors when trying to do the following.
I have a FileMgr class to handle an input and an output file with two member functions to copy each line of input into a list and to write to the output from each member of a list. note: the following functions work properly when handled directly by my main! so don't bother trying to make out what I'm doing with the copy functions, I spent a lot of time figuring them out and now they work fine, the problem is not there.
FileMgr::FileMgr(string inFilename, string outFilename)
{
input.open(inFilename);
output.open(outFilename);
}
bool FileMgr::writeFileToList(list<string> &l)
{
// copy each line of file into new member of list<string>
if(!input.is_open())
return false;
copy(istream_iterator<string>(input), istream_iterator<string>(), back_inserter(l));
return true;
}
bool FileMgr::writeListToFile(list<string>::iterator begin, list<string>::iterator end)
{
// copy each member of list<string> in output file, beginning and ending at iterators begin, end
// note that I have to pass a "false" end iterator, that is, end--, for it to work
if(!output.is_open())
return false;
copy(begin, end, ostream_iterator<string>(output, "\n"));
return true;
}
and up to here everything is fine. then my other class, which gets the list from FileMgr, and it's supposed to let the user edit it (im not there yet because of these errors), so heres part of my declaration:
class Dictionary
{
public:
Dictionary(string inFileName = "dictionary.txt", string outFileName = "output.txt");
void userEditor();
//private:
list<string> dictionary;
FileMgr manager;
bool findWord(string word);
bool addWord(string word);
bool deleteWord(string word);
void sortAndFix();
void saveAndExit();
and here's my definitions so far, which is basically just the constructor:
Dictionary::Dictionary(string inFileName, string outFileName)
{
// open files and copy to list; sort and fix list.
manager = FileMgr(inFileName, outFileName);
dictionary.push_back(" ");
if( manager.writeFileToList(dictionary) )
cout << "File successfully read from " << inFileName << endl;
else
cout << "Error in reading " << inFileName << endl;
sortAndFix();
}
when I compile, I get these errors somewhere unknown in the constructor just shown (because it's the only code in the file I get these errors from when compiling):
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(860): error
C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator =(const std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(604): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
I can't understand what's wrong. my FileMgr works fine when tested from my main, so why would the compiler trip like that when working with FileMgr from another class??
I believe that your problem is in this line:
manager = FileMgr(inFileName, outFileName);
From your code in FileMgr it seems that FileMgr has a stream input as a data member. When you execute the above line, you'll invoke the assignment operator for FileMgr, which by default will try to copy all of the data members one at a time. However, the copy functions for streams are not accessible (they're marked private and not implemented). The errors you're getting are almost certainly due to the C++ compiler noticing that it needs to copy the streams, but failing to do so because the copy functions are inaccessible.
To change this, try initializing manager in the constructor's member initializer list:
Dictionary::Dictionary(string inFileName, string outFileName)
: manager(inFileName, outFileName) {
/* ... */
}
This will initialize manager with the given parameters rather than trying to assign manager an object with the right parameters.
Hope this helps!
bool FileMgr::writeFileToList(list<string> &l);
FileMgr::writeFileToList receive an argument of type list<string> by reference.
So you should actually do -
list<string> dictionary;
if( manager.writeFileToList(&dictionary) )
^ error. You should not use & symbol here.
The argument type is not list<string>*l to send an address.
Just write:
if( manager.writeFileToList(dictionary) );
& is not needed. In fact, that causes the error!
BTW, your std::copy is incorrectly written. Here is the correct one:
copy(istream_iterator<string>(input), istream_iterator<string>(), std::back_inserter(l));
Note the last argument. It's std::back_inserter(l).
if( manager.writeFileToList(&dictionary) ) should be changed to
if( manager.writeFileToList(dictionary) )
Note you cant convert Type* to Type&
Related
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.
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()?
This is a simple and complex question at the same time.
This compiles:
int Test;
vector<int> TEST;
TEST.push_back(Test);
cout << TEST.size();
This does not compile:
fstream Test;
vector<fstream> TEST;
TEST.push_back(Test);
cout << TEST.size();
Is there any particular reason?
Is there a way for me to get a dynamic list of fstreams?
The error message:
1>------ Build started: Project: vector_test, Configuration: Debug Win32 ------
1> vector_test.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(1347): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_fstream<_Elem,_Traits>::basic_fstream(const std::basic_fstream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The object fstream is not copyable.
If you need to record fstreams in a vector you can declare a std::vector<std::fstream*> and push back the address of the object. Remember that if you save the pointer, then you must ensure that, when you access it, the object is still alive.
In C++ 2011 the concrete stream objects are movable. Howver, to take advantage of this you either need to pass a temporary or allow the object to be moved:
std::vector<std::ofstream> files;
files.push_back(std::ofstream("f1"));
std::ofstream file("f2");
files.push_back(std::move(file));
Note that you can't use the file variable after this as the stream was moved into files.
To use a class with a vector, it must be copyable. fstream is not.
See: C++ std::ifstream in constructor problem
Edit: If you need to have multiple references to the same fstream, you can use shared_ptr to manage them. Try something like:
std::vector< std::shared_ptr<fstream> > TEST
A basic requirement for a type to be pushed into vector is that the object should be copyable, fstream is not copyable and hence you get compiler errors.
Like others have mentioned, fstream is not copyable. You could just do:
vector<fstream> TEST;
TEST.push_back(fstream()); //fstream is created in the vector, no copy needed
And use like this:
fstream& A = TEST[0];
And even iterate like this:
for(fstream& S : TEST){
...
}
I am facing strange problem.Same code is working fine in vs 2008 and VS 2010 Debug and Dubug Unicode Version but failed to compile in Release and Release Unicode.
What could be the reason for this.
This code is generating the error
struct bitset_extractor
{
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef ptrdiff_t difference_type;
bitset_extractor(const boost::dynamic_bitset<T>& bs, T *buffer)
: bs_(bs), buffer_(buffer), current_(0)
{}
bitset_extractor(const bitset_extractor& it)
: bs_(it.bs_), buffer_(it.buffer_), current_(it.current_)
{}
T& operator*()
{
return buffer_[current_];
}
bitset_extractor& operator++()
{
++current_;
return *this;
}
private:
void operator=(T const&); // unimplemented
const boost::dynamic_bitset<T>& bs_;
T * const buffer_;
unsigned int current_;
};
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xutility(275): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'bitstream::bitset_extractor<T>' (or there is no acceptable conversion)
1> with
1> [
1> T=uint8_t
1> ]
1> C:\vikram\Project\Seurat\src\app\logitech\LogiRTP\Library\Filters\plc\common\bitstream.h(195): could be 'void bitstream::bitset_extractor<T>::operator =(const T &)'
1> with
1> [
1> T=uint8_t
1> ]
1> while trying to match the argument list '(bitstream::bitset_extractor<T>, bitstream::bitset_extractor<T>)'
1> with
1> [
1> T=uint8_t
1> ]
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xutility(2176) : see reference to function template instantiation '_Iter &std::_Rechecked<_OutIt,_OutIt>(_Iter &,_UIter)' being compiled
1> with
1> [
1> _Iter=bitstream::bitset_extractor<uint8_t>,
1> _OutIt=bitstream::bitset_extractor<uint8_t>,
1> _UIter=bitstream::bitset_extractor<uint8_t>
1> ]
1> C:\vikram\Project\Seurat\3rdparty\boost\boost/dynamic_bitset/dynamic_bitset.hpp(1090) : see reference to function template instantiation '_OutIt std::copy<std::_Vector_const_iterator<_Myvec>,BlockOutputIterator>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=bitstream::bitset_extractor<uint8_t>,
1> _Myvec=std::_Vector_val<unsigned char,std::allocator<uint8_t>>,
1> BlockOutputIterator=bitstream::bitset_extractor<uint8_t>,
1> _InIt=std::_Vector_const_iterator<std::_Vector_val<unsigned char,std::allocator<uint8_t>>>
1> ]
1> C:\vikram\Project\Seurat\src\app\logitech\LogiRTP\Library\Filters\plc\common\bitstream.h(210) : see reference to function template instantiation 'void boost::to_block_range<uint8_t,std::allocator<_Ty>,bitstream::bitset_extractor<T>>(const boost::dynamic_bitset<Block> &,BlockOutputIterator)' being compiled
1> with
1> [
1> _Ty=uint8_t,
1> T=uint8_t,
1> Block=uint8_t,
1> BlockOutputIterator=bitstream::bitset_extractor<uint8_t>
1> ]
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xutility(275): error C2582: 'operator =' function is unavailable in 'bitstream::bitset_extractor<T>'
1> with
1> [
1> T=uint8_t
1> ]
The problem is that your bitset_extractor is being used as an iterator, but it doesn't meet all of the requirements for an iterator.
The std::copy function is calling operator= with two bitset_extractor<uint8_t> objects as it attempts to convert the original iterator into a checked iterator. Since no checked iterator exists for user defined iterators, the checked iterator type and original iterator type are the same, resulting in a regular copy of the iterator being used.
The culprit is the _Rechecked function, which is used to convert a regular iterator into a checked iterator. This is done differently, depending on the iterator debug level; that's why your Debug build works, but not your Release build, as they have different iterator debug levels by default.
The solution is to implement operator= for bitset_extractor. If you want to use it as an iterator, it must support all of the functionality required for an iterator of it's type.
Disabling checked iterators won't help. Your iterators will still go through the _Rechecked function, no matter what you do.
Problem is solved .I added _SECURE_SCL=1 in VS Project Setting->preprocessor.
It is working fine.
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