C++ Function was not declared in this scope error - c++

I am compiling a code, but I seem to get an error, even as I examine my code, I find it weird that I'm getting this error.
My code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "2A";
int n = stoul(s, nullptr, 16);
cout << n << endl;
return 0;
}
and in the command line, I'm typing g++ -std=c++11 main.cpp, I'm using -std=c++11 because the function stoul is from C++11 according to this page stoul.
The error I'm getting is the following:
main.cpp: In function 'int main()':
main.cpp:8:30: error: 'stoul' was not declared in this scope
int n = stoul(s, nullptr, 16);
^
Why am I getting this error?

Related

rand() in MinGW g++

I have a basic C++ file like so:
#include <iostream>
using namespace std;
int main() {
float x = rand();
cout << x << endl;
return 0;
}
When I run this through g++ on Ubuntu with g++ test.cpp -o test -std=c++11, I get no errors, and the program runs just fine. But when I run it through g++ on MinGW with the same command, I get the following error:
test.cpp: In function 'int main()':
test.cpp:6:17: error: 'rand' was not declared in this scope
float x = rand();
^
I have GCC version 5.3.0. Attempting to compile with g++ test.cpp -o test.exe -std=gnu++11 or g++ test.cpp -o test.exe -std=c++0x yield the same result.
You must include library for the random function first
i-e
#include < cstdlib >
After that your code will work perfectly
Here is the correct code
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
float x = rand();
cout << x << endl;
return 0;
}
Some compilers allow you to use random function without including library but standard C++ compilers doesn't allow you.
Hope this will help you

error: 'to_string' was not declared in this scope in Windows using cmd

I'm trying compile this code in cmd:
#include <iostream>
#include <string>
using namespace std;
int main(){
int n = 24;
string s = to_string(n);
cout<<s;
}
I found in another qestions, that I should use this command:
g++ -std=c++11 main.cpp
but I get error:
main.cpp: In function 'int main()':
main.cpp:7:24: error: 'to_string' was not declared in this scope
string s = to_string(n);
^
I have latest g++ compiler for Windows v. 5.3.0. I used this installer for MinGW.
I tried solve in to_string is not a member of std, says g++, but this patch doesn't work.
How can I compile this code in command line in Windows?

FI_Init_FreeType variable declaration error

I am trying to write a simple program that uses freetype.
code :
#include <ft2build.h>
#include FT_FREETYPE_H
int main()
{
FT_Library library;
int error = FI_Init_FreeType(&library);
}
i am compiling as g++ try1_c++demo.cpp -I/usr/include/freetype2
it gives following error:
try1_c++demo.cpp: In function ‘int main()’:
try1_c++demo.cpp:9: error: ‘FI_Init_FreeType’ was not declared in this scope
how do i solve this error?

GCC cannot compile: '* does not name a type'

Today, after Slackware 13.37 installation, i've got the problem: default GCC 4.5.2 cannot compile my code. Now I study C++ by the Stephen Davis's book "C++ for dummies" and want to compile this:
#include <stdio.h>
#include <iostream.h>
int main(int nNumberofArgs, char* pszArgs[])
{
int nNCelsius;
cout << "Celsisus: ";
cin >> nNCelsius;
int nNFactor;
nNFactor = 212 - 32;
int nFahrenheit;
nFahrenheit = nNFactor * nNCelsius / 100 + 32;
cout << "Fahrenheit: ";
cout << nFahrenheit;
return 0;
}
But my GCC 4.5.2 gives these errors:
FahTCel.cpp:7:14: error: expected ')' before ';' token
FahTCel.cpp:7:14: error: 'main' declared as function returning a function
FahTCel.cpp:8:1: error: 'cout' does not name a type
FahTCel.cpp:9:1: error: 'cin' does not name a type
FahTCel.cpp:12:1: error: 'nNFactor' does not name a type
FahTCel.cpp:15:1: error: 'nFahrenheit' does not name a type
FahTCel.cpp:17:1: error: 'cout' does not name a type
FahTCel.cpp:18:1: error: 'cout' does not name a type
FahTCel.cpp:20:1: error: expected unqualified-id before 'return'
FahTCel.cpp:21:1: error: expected declaration before '}' token
Three errors:
The correct header is <iostream>. This program requires no other headers.
You must either put using namespace std; in the file, or refer to std::cout and std::cin explicitly. Take your pick, plenty of C++ programmers disagree about which of the two options is better. (You could also bring just cin and cout into your namespace, if you wanted.)
The program does not write a line terminator at the end. This will cause the output to "look bad" on most terminals, with the command prompt appearing on the same line as the output. For example:
Here are the corrections:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
...
cout << nFahrenheit << '\n';
...
}
Note: It is extremely unusual to see main take parameters with names other than argc and argv. Changing the names just makes it harder for other people to read your code.
its std::cout or you should add using namespace std;
and the include should be < iostream> not < ionstream.h>.

Unable to compile simple jsoncpp program with Eclipse on LInux

The File is at the location /home/shivang/Desktop and the filename is sh1.cpp
Source code for the file is given below
#include iostream
#include json/json.h
#include json/reader.h
using namespace std;
using namespace Json;
int main() {
std::string example = "{\"array\":[\"item1\", \"item2\"], \"not an array\":\"asdf\"}";
Value value;
Reader reader;
bool parsed = reader.parse(example, value, false);
std::cout << parsed;
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
The following error messages are displayed.
/home/shivang/Desktop/sh1.cpp: In function ‘int main()’:
/home/shivang/Desktop/sh1.cpp:10:2: error: ‘Value’ was not declared in this scope
/home/shivang/Desktop/sh1.cpp:10:8: error: expected ‘;’ before ‘value’
/home/shivang/Desktop/sh1.cpp:11:2: error: ‘Reader’ was not declared in this scope
/home/shivang/Desktop/sh1.cpp:11:9: error: expected ‘;’ before ‘reader’
/home/shivang/Desktop/sh1.cpp:13:16: error: ‘reader’ was not declared in this scope
/home/shivang/Desktop/sh1.cpp:13:38: error: ‘value’ was not declared in this scope
Configuration gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
jsoncpp-src-0.5.0
eclipse-cpp-helios-SR2-linux-gtk
I have never used Json or C++ before. But a little googling around led me to this page. I think adding the following line to your list of includes should help:
#include <json/value.h>