when I try to compile my program I get this error:
error: ‘_byteswap_ushort’ was not declared in this scope
long lNum = (long)_byteswap_ushort(iNum);
this is the program:
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main()
{
long inputNum;
cout << "Input number:\n";
cin >> inputNum;
long Num = (long)_byteswap_ulong(iNum);
stringstream oss;
oss << hex << Num;
string mystring = oss.str();
return 0;
}
I thought that including stdlib should solve the problem. Is there any other library I should include?
The program is compiled with:
g++ -m32 -o output32 prog.cpp
You seem to be trying to use a function specific to MS Visual C++ compiler, which is not available in GCC. Use an appropriate GCC builtin instead.
Seems like uint32_t __builtin_bswap32(uint32_t x) would be appropriate in this case.
Another option, if you wanted a portable solution, would be to use something like Boost Endian library.
Related
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}
I get the error message "stoi is not a member of std" when I try to use std::stoi and try to compile it. I'm using g++ 4.7.2 from the command line so it can't be IDE error, I have all my includes in order, and g++4.7.2 defaults to using c++11. If it helps, my OS is Ubuntu 12.10. Is there something I haven't configured?
#include <iostream>
#include <string>
using namespace std;
int main(){
string theAnswer = "42";
int ans = std::stoi(theAnswer, 0, 10);
cout << "The answer to everything is " << ans << endl;
}
Will not compile. But there's nothing wrong with it.
std::stoi() is new in C++11 so you have to make sure you compile it with:
g++ -std=c++11 example.cpp
or
g++ -std=c++0x example.cpp
For older version of C++ compiler does not support stoi. for the older version you can use the following code snippet to convert a string to integer.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
string input;
cin >> input;
int s = std::atoi(input.c_str());
cout<<s<<endl;
return 0;
}
I #include these headers:
#include <iostream>
#include <fstream>
but however this piece of code:
ifstream inFile;
still wont compile. what could be the problem? Im using Visual Studio 2010, Win32 C++.
You can put a using namespace std; at the top of your code so you don't have to fully qualify standard C++ stuff, but it's considered bad form by a large number of developers.
I simply prefix the standard stuff with std::, which makes the code longer:
std::cout << "Hello, world.\n";
but keeps me out of trouble vis-a-vis namespace clashes.
The following transcript shows the use of std:: prefixes in action:
$ cat testprog.cpp
#include <iostream>
#include <fstream>
int main (void) {
int n;
std::ifstream inFile("input.txt");
inFile >> n;
std::cout << "File contained " << n << '\n';
return 0;
}
$ cat input.txt
42
$ g++ -Wall -Wextra -o testprog testprog.cpp ; ./testprog
File contained 42
The type is std::ifstream. You must write it out in full, unless you brought the qualified name into scope by another means.
I'm trying to run my very first c++ program in linux (linux mint 8). I use either gcc or g++, both with the same problem: the compiler does not find the library I am trying to import.
I suspect something like I should either copy the iostream.h file (which I don't know where to look for) in the working folder, move my file to compile somewhere else or use an option of some sort.
Thanks for your suggestions.
Here's the gcc command, the c++ code, and the error message:
gcc -o addition listing2.5.c
.
#include <iostream.h>
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
cout << "Resultat : " << Addition(2, 4) << "\n";
return 0;
}
.
listing2.5.c:1:22: error: iostream.h: No such file or directory
listing2.5.c: In function ‘main’:
listing2.5.c:10: error: ‘cout’ undeclared (first use in this function)
listing2.5.c:10: error: (Each undeclared identifier is reported only once
listing2.5.c:10: error: for each function it appears in.)
Now the code compiles, but I cannot run it from the command line using the file name. addition: command not found Any suggestion?
cout is defined in the std:: namespace, you need to use std::cout instead of just cout.
You should also use #include <iostream> not the old iostream.h
use g++ to compile C++ programs, it'll link in the standard c++ library. gcc will not. gcc will also compile your code as C code if you give it a .c suffix. Give your files a .cpp suffix.
please use g++ not gcc to compile it
You need <iostream> not <iostream.h>.
They are also header files not libraries.
Other things to fix, cout should be std::cout and you should use std::endl instead of "\n".
You need <iostream>, <iostream.h> is non-standard too-old header. Try this:
#include <iostream>
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
using namespace std;
cout << "Resultat : " << Addition(2, 4) << "\n";
return 0;
}
If you don't want to use std alongside cout as below-
std::cout << "Hello World";
You can also define std at beginning of program by 'using namespace' keywords as-
#include <iostream >
using namespace std;
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
cout << "Result : " << Addition(2, 4) << "\n";
return 0;
}
Now you need not to write std,everytime you use I/O operations.