This may be a vaugue question but im getting this error
Simulator.cpp: In member function `void Simulator::generatePassengers()':
Simulator.cpp:60: error: `itoa' undeclared (first use this function)
This is the code im having an issue with
itoa(i,buf,10);
What can i use to fix this issue because on one compiler i get this issue on another i dont. So im stumped here has to work on both.
char buf[3];
if(i<10)
{
key1.append("0");
key2.append("0");
key3.append("0");
}
itoa(i,buf,10);
key1.append(buf);
key2.append(buf);
key3.append(buf);
Your platform doesn't have itoa. It's not specified by any standard. You can use any replacement you like, including printf or just writing your own.
Does this work?
#include <string>
std::string buf = std::to_string(i);
key1.append(buf);
key2.append(buf);
key3.append(buf);
Related
we have following code in legacy and use this place hundread of place. I am compiling code with c++11 and got following error. I can understand issue(saw couple of question on stackoverflow) as abs support int/long int in C++11.
Is there any way to avoid 100 place and replace abs with fabs. Can I update such a way if it can handle both version. any input.
call of overloaded ‘abs(double&)’ is ambiguous
double abs(double d)
{
return (d < 0 ? -d : d);
}
Why redefine what is already defined?
You can find the function that you want in the header <cmath>.
Further reading on c++ reference.
#include <cmath>
add this header file and try to use abs() built-in function and see that work or not.
I am writing a program in C++. When I use the strlen function, it is underlined by a red line. Although the project is built without errors. This is how I use this function. (By the way, strcpy is also underlined).
Exception::Exception(int _Line, char* _File, char* _Func, char* _Desc)
{
Line = _Line;
int size = strlen(_File) + 1;
File = new char[size];
strcpy(File, _File);
Func = new char[size];
strcpy(Func, _Func);
Desc = new char[size];
strcpy(Desc, _Desc);
}
And I declared <cstring> library at the beginning of the file. Please tell me how I can fix this?
As you can read up in the documentation for strlen(), strlen() is defined in string.h and the C++ counterpart std::strlen() is defined in cstring.
So to get rid of the error squiggles try adding one of the aforementioned headers.
#include <cstring>
...
std::strlen()
The system that underlines the code in the VS editor, called IntelliSense, does not use the same code as the compiler itself (or at least did not a few years ago when I used it last time). Sometimes it gets confused.
Try std::strlen instead, reorganizing the code, the includes, or something else.
Pacient& operator = ( Pacient&p) {
cout << "Operator = Pacient" << endl;
delete [] this->nume;
this->nume = new char[strlen(p.nume)+1];
strcpy_y(this->nume,strlen(p.nume)+1,p.nume); // ERROR
delete [] this->prenume;
this->prenume = new char[strlen(p.prenume)+1];
strcpy_y(this->prenume,strlen(p.prenume)+1,p.prenume); // ERROR
this->varsta = p.varsta;
return *this;
}
1>Compiling...
1>pacienti.cpp
1>f:\bob\facultate\semestrul iii\programareorientataobiect\proiect pacienti\proiect\proiect\pacienti.cpp(24) : error C3861: 'strcpy_y': identifier not found
1>f:\bob\facultate\semestrul iii\programareorientataobiect\proiect pacienti\proiect\proiect\pacienti.cpp(27) : error C3861: 'strcpy_y': identifier not found
I get this error. Why? What should I do to get rid of it?
The compiler doesn't know what strcpy_y is because you haven't declared it anywhere prior to that line in the current translation unit.
Either provide the declaration for strcpy_y (e.g. #include a relevant header, or forward declare it), or fix the typo if you meant to call some other function.
There's no such thing as strcpy_y, at least not in Standard C++.
Perhaps you meant strcpy_s from WINAPI?
If strcpy_y is a function you have created yourself, then you need to #include the file where it is declared.
Why use raw C-style strings in the first place? Just use std::string allocated as a locat variable and all these problems go away. Use operator= to copy the strings.
I think that it can be help you:
#include <string.h>
But I don't recommended use strcpy_y in your code, because it's not C++.
If I instantiate a mapped_file_source (boost 1.46.1 ) with a narrow character string as in the following I don't have a problem:
boost::iostreams::mapped_file_source m_file_( "testfile.txt" );
However if I try to use a wide string:
boost::iostreams::mapped_file_source m_file_( L"testfile.txt" );
I get the following compiler error in VC2010 SP1:
P:\libs\boost_1_46_1\boost/iostreams/device/mapped_file.hpp(128): error C2248: 'boost::iostreams::detail::path::path' : cannot access private member declared in class 'boost::iostreams::detail::path'
P:\libs\boost_1_46_1\boost/iostreams/detail/path.hpp(111) : see declaration of 'boost::iostreams::detail::path::path'>
P:\libs\boost_1_46_1\boost/iostreams/detail/path.hpp(37) : see declaration of 'boost::iostreams::detail::path'
If I instead try to pass the constructor a boost::filesystem::path I get the following error:
P:\libs\boost_1_46_1\boost/iostreams/device/mapped_file.hpp(128): error C2664: 'boost::iostreams::detail::path::path(const std::string &)' : cannot convert parameter 1 from 'const boost::filesystem3::path' to 'const std::string &'
Reason: cannot convert from 'const boost::filesystem3::path' to 'const std::string'
I feel like I'm missing something obvious, but I'm just running around in circles trying to figure out what the compiler is trying to tell me, but I'm just getting lost. That palm to forehead moment is just not happening.. What is it that I am doing incorrectly?
The constructor defined in mapped_file.hpp looks like the following:
// Constructor taking a parameters object
template<typename Path>
explicit mapped_file_source(const basic_mapped_file_params<Path>& p);
The basic_mapped_file_params class constructors look like this:
// Construction from a Path
explicit basic_mapped_file_params(const Path& p) : path(p) { }
// Construction from a path of a different type
template<typename PathT>
explicit basic_mapped_file_params(const PathT& p) : path(p) { }
Where the template class is defined as:
// This template allows Boost.Filesystem paths to be specified when creating or
// reopening a memory mapped file, without creating a dependence on
// Boost.Filesystem. Possible values of Path include std::string,
// boost::filesystem::path, boost::filesystem::wpath,
// and boost::iostreams::detail::path (used to store either a std::string or a
// std::wstring).
template<typename Path>
struct basic_mapped_file_params
: detail::mapped_file_params_base
{
There is some additional help in the header that says:
// For wide paths, instantiate basic_mapped_file_params
// with boost::filesystem::wpath
If I take this approach with:
boost::iostreams::basic_mapped_file_params<boost::filesystem::wpath> _tmp(L"test.txt");
boost::iostreams::mapped_file_source m_file_( _tmp );
I get the same C2664 error mentioned above..
I know the compiler is telling me what the problem is, but looking at the header source and the comments leads me to believe that what I'm trying to accomplish is supported, it's just my approach that is incorrect. Am I misinterpreting what the header file is telling me? I know there is probably a good lesson about template instantiation and explicit/implicit conversion in here somewhere.
Interestingly enough, upgrading my boost install to 1.47.0 seems to cleared up C2664 error but I'm still getting the C2248 error about access to the private member.
With boost 1.48 I can do something like this.
#include <boost/filesystem.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <iostream>
int main()
{
boost::filesystem::path p(L"b.cpp");
boost::iostreams::mapped_file file(p); // or mapped_file_source
std::cout << file.data() << std::endl;
}
or you can do this with mapped_file_params(used create new file)
boost::filesystem::path p(L"aa");
basic_mapped_file_params<boost::filesystem::path> param; // template param
param.path = p;
param.new_file_size = 1024;
It's telling you that boost::iostreams::mapped_file_source's constructor does not take a wchar_t*, nor does it take a boost::filesystem::path. It only takes std::string, or types convertible to std::string. Or, to put it another way, you can't use UTF-16 paths with this object.
It looks like the documentation for mapped_file is pretty old and does not reflect what is in the header or in the header comments. In order to instantiate a boost::iostreams:mapped_file_source object with a wide character string you need to explicity pass in the boost::iostreams::detail::path like this:
boost::iostreams::mapped_file_source m_file_( boost::iostreams::detail::path(boost::filesystem::path(L"testfile.txt")) );
I was able to get this to compile by stepping thought the error messages and determining how the template classes were being instantiated and finally saw that boost::iostreams::detail::path had a private constructor that took a &std::wstring as a parameter which is where the code was failing to compile.
My problem is that when I want to make a downloaded library I get some weird compile errors from GCC and the code that the compiler demands to correct seems just to be right.
The errors are all like this:
Catalogue.h:96: error: there are no
arguments to ‘strlen’ that depend on a
template parameter, so a declaration
of ‘strlen’ must be available
Here is the code around line 96:
GaCatalogueEntry(const char* name, T* data)
{
if( name )
{
_nameLength = (int)strlen( name ); // LINE 96
// copy name
_name = new char[ _nameLength + 1 ];
strcpy( _name, name ); // LINE 100: similar error
_data = data;
return;
}
_name = NULL;
_nameLength = 0;
_data = NULL;
}
What can I do to fix these compile errors?
You probably just need to include the header that contains the strcpy and strlen library functions.
#include <string.h>
or (preferably for C++)
#include <cstring>
In C++ the strlen() function is part of the string library, and it almost looks like the header file was not included.
Is it included anywhere?
include <string.h>
If not, try adding it and see if that fixes the problem.
The code is buggy. You are probably missing an #include <string.h>.
If you don't want to change the code, add -fpermissive to the compiler options. (See the GCC documentation.)
a declaration of ‘strlen’ must be available
Include string.h or <cstring> (C++) for the declaration of strlen().