Why is g++ not able to compile tuples? [duplicate] - c++

This question already has answers here:
error: ‘i’ does not name a type with auto [duplicate]
(2 answers)
Closed 6 years ago.
#include <iostream>
#include <tuple>
#include <string>
using namespace std;
int main(){
tuple<string, string, string> x;
x = make_tuple("hi", "a", "b");
cout << get<0>(x) << endl << endl;
}
I've been having difficulties with my program, so I wrote a simpler one and even this does not work. I do not understand why there is a problem after reviewing the documentation several times. It also compiles fine on XCode but for some reason breaks down on g++.
Here is the full error message:
test.cpp:6:3: error: use of undeclared identifier 'tuple'
tuple x;
^
test.cpp:6:9: error: unexpected type name 'string': expected
expression
tuple x;
^
test.cpp:7:3: error: use of undeclared identifier 'x'
x = make_tuple("hi", "a", "b");
^
test.cpp:7:7: error: use of undeclared identifier 'make_tuple'
x = make_tuple("hi", "a", "b");
^
test.cpp:8:11: error: reference to overloaded function could not be
resolved; did you mean to call it? cout << get<0>x << endl << endl;
The command I am using is g++ test.cpp

Try #include <string>.
Possibly (depending on your version og gcc) you also need -std=c++11 on the command line.

The tuple is fine; what you're trying to make it a tuple of is not.
You did not #include <string>!
Thus the word "string" means nothing to your compiler, and it has no idea what you want it to do. It can't even tell that you meant it to be a type, so it can't tell that by the word "tuple" you meant "std::tuple". So on, and so forth…

Related

(Learning C++) Why is my compiler reporting errors when I try to make a tuple?

I'm new to c++ and have taken the liberty to learn it this summer after coming from a python background. I was watching a video about how to create and use tuples within c++ and it seemed to have worked for the YouTuber, however when I replicated his steps, my compiler had thrown some errors even though there was no distinct differences in the code
code:
#include <iostream>
#include <string>
#include <tuple>
int main() {
std::tuple <int, std::string> person(18, "Chris");
std::cout << std::get<1>(person) << std::endl;
return 0;
}
Errors:
❯ g++ -o main Tuples.cpp && ./main
Tuples.cpp:7:10: error: no member named 'tuple' in namespace 'std'
std::tuple <int, std::string> person(18, "Chris");
~~~~~^
Tuples.cpp:7:20: error: expected '(' for function-style cast or type construction
std::tuple <int, std::string> person(18, "Chris");
~~~^
Tuples.cpp:8:30: error: use of undeclared identifier 'person'
std::cout << std::get<1>(person) << std::endl;
^
3 errors generated.
Video for reference: https://www.youtube.com/watch?v=T9-agjKW4PQ&list=PLzMcBGfZo4-lmGC8VW0iu6qfMHjy7gLQ3&index=16

How to write a program that displays numeric limits of a data type in a table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm currently learning how to program in C++ and one of the practical examples is to write a program showing the data types numeric limit in a table. Currently writing in repl.it before pasting to .txt and compiling using makefile. There are no resources or similar examples I could find explaining how to do this, nor have I fully grasped enough to ID the correct keywords to use.
Question
Attempt:
#include <limits>
void main()
{
number = sizeor(int)
unsign = sizeor(unsigned int)
long = sizeor(long)
longlong = sizeor(long_long)
unsignedchar = sizeor(unsigned char)
float = sizeor(float)
double = sizeor(double)
char = sizeor(char)
cout<<numeric_limits<int>::min();
cout<<numeric_limits<int>::max();
cout<<numeric_limits<int>::epsilon();
cout<<numeric_limits<double>::min();
cout<<numeric_limits<double>::max();
cout<<numeric_limits<double>::epsilon();
cout<<numeric_limits<unsign>::min();
cout<<numeric_limits<unsign>::max();
cout<<numeric_limits<unsign>::epsilon();
cout<<numeric_limits<long>::min();
cout<<numeric_limits<long>::max();
cout<<numeric_limits<long>::epsilon();
cout<<numeric_limits<longlong>::min();
cout<<numeric_limits<longlong>::max();
cout<<numeric_limits<longlong>::epsilon();
cout<<numeric_limits<unsignedchar>::min();
cout<<numeric_limits<unsignedchar>::max();
cout<<numeric_limits<unsignedchar>::epsilon();
cout<<numeric_limits<float>::min();
cout<<numeric_limits<float>::max();
cout<<numeric_limits<float>::epsilon();
cout<<numeric_limits<char>::min();
cout<<numeric_limits<char>::max();
cout<<numeric_limits<char>::epsilon();
}
Except this prints
main.cpp:5:2: error: expected function body after function declarator
number = sizeor(int)
^
main.cpp:15:2: error: unknown type name 'cout'
cout<<numeric_limits<int>::max();
^
main.cpp:15:6: error: expected unqualified-id
cout<<numeric_limits<int>::max();
^
main.cpp:16:2: error: unknown type name 'cout'
cout<<numeric_limits<int>::epsilon();
^
main.cpp:16:6: error: expected unqualified-id
cout<<numeric_limits<int>::epsilon();
^
main.cpp:18:2: error: unknown type name 'cout'
cout<<numeric_limits<double>::min();
^
main.cpp:18:6: error: expected unqualified-id
cout<<numeric_limits<double>::min();
^
main.cpp:19:2: error: unknown type name 'cout'
cout<<numeric_limits<double>::max();
^
main.cpp:19:6: error: expected unqualified-id
cout<<numeric_limits<double>::max();
^
main.cpp:20:2: error: unknown type name 'cout'
cout<<numeric_limits<double>::epsilon();
^
main.cpp:20:6: error: expected unqualified-id
cout<<numeric_limits<double>::epsilon();
^
main.cpp:22:2: error: unknown type name 'cout'
cout<<numeric_limits<unsign>::min();
^
main.cpp:22:6: error: expected unqualified-id
cout<<numeric_limits<unsign>::min();
^
main.cpp:23:2: error: unknown type name 'cout'
cout<<numeric_limits<unsign>::max();
^
main.cpp:23:6: error: expected unqualified-id
cout<<numeric_limits<unsign>::max();
^
main.cpp:24:2: error: unknown type name 'cout'
cout<<numeric_limits<unsign>::epsilon();
^
main.cpp:24:6: error: expected unqualified-id
cout<<numeric_limits<unsign>::epsilon();
^
main.cpp:26:2: error: unknown type name 'cout'
cout<<numeric_limits<long>::min();
^
main.cpp:26:6: error: expected unqualified-id
cout<<numeric_limits<long>::min();
What is the correct method to acquire the desired result and where/what are the deficiencies in my attempt that I need to correct?
There're quite a lot of errors/typos within your code.
void main() will return a '::main' must return 'int' error, the correct syntax is int main().
number = (IMO is Pythonic syntax) should be int number = , as in C++, the correct format for declaring variables is type variable_name = value;. More info here.
Variables declarations should all end with ;, or the program will return a error: expected ',' or ';' before 'int'.
A keyword such as int or char mustn't be used as a variable name.
sizeor() is not a valid operator, it's sizeof() as specified clearly in your question.
cout and numeric_limits<int>::min() is invalid, it should be std::cout and std::numeric_limits<int>::min(), or you can use using namespace std; instead. Also, when using cout, #include <iostream> must be used.
Some of the type you used is invalid, such as long_long should be long long, unsign should be unsigned int and unsignedchar should be unsigned char.
I include a little snippet of your code which have been modified here (you can build and fill in the rest from this):
#include <limits>
#include <iostream>
int main()
{
int number = sizeof(int);
int unsign = sizeof(unsigned int);
int long_ = sizeof(long);
int longlong = sizeof(long long);
int unsignedchar = sizeof(unsigned char);
int float_ = sizeof(float);
int double_ = sizeof(double);
int char_ = sizeof(char);
std::cout<<std::numeric_limits<int>::min() << "\n";
std::cout<<std::numeric_limits<unsigned char>::min() << "\n";
std::cout<<std::numeric_limits<long long>::min() << "\n";
std::cout<<std::numeric_limits<float>::min() << "\n";
}
Result:
-2147483648
-9223372036854775808
1.17549e-38
It should also be noticed that sizeof() actually "Yields the size in bytes of the object representation of type", not bits. As #Nathan Pierson mentioned, the number of bits is 8 times more than the number of bytes.
P.S: As #tadman mentioned above:
Instead of typing all of this code in and then mashing the compile
button, start with the most minimal program, ensure it compiles, and
build incrementally from there. Compile frequently. Stop adding more
code the instant you have a compile problem, fix that issue before
making things worse.
And #JaMiT :
Building on what tadman wrote, your first error is on the first line
in your main function. Try reducing your program to #include <limits> void main() { number = sizeor(int) } and focusing on what is
preventing that much from compiling. (A minimal reproducible example
is a powerful debugging tool; it doesn't matter that there is no
useful functionality yet.)
IMO, you're not really familiar with C++ syntaxes, maybe you should try some simpler program first to get the hang of it. The list of books #Richard Critten provided is very useful for beginners.
More info:
std::numeric_limits : https://en.cppreference.com/w/cpp/types/numeric_limits
sizeof() : https://en.cppreference.com/w/cpp/language/sizeof
std::cout : https://en.cppreference.com/w/cpp/io/cout
sizeof is an operator and keyword in c++, and it calculates the bytes of data type or variables during the complying time. remember that sizeof is an operator, not a function.

Using unordered_map on my g++ (5.1.0) compiler in command prompt shows error

I have recently downloaded MinGW into my computer but on using certain containers and iterators like unordered_map and auto it shows an unexpected error.
my code is as follows :
#include <bits/stdc++.h>
#include<unordered_map>
using namespace std;
int main()
{
unordered_map<string, int> umap;
umap["GeeksforGeeks"] = 10;
umap["Practice"] = 20;
umap["Contribute"] = 30;
for (auto x : umap)
cout << x.first << " " << x.second << endl;
return 0;
}
it gives the following error :
C:\Users\naima\Documents\cpp>g++ -o try2 try2.cpp
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/unordered_map:35:0,
from try2.cpp:2:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
try2.cpp: In function 'int main()':
try2.cpp:9:5: error: 'unordered_map' was not declared in this scope
unordered_map<string, int> umap;
^
try2.cpp:9:25: error: expected primary-expression before ',' token
unordered_map<string, int> umap;
^
try2.cpp:9:27: error: expected primary-expression before 'int'
unordered_map<string, int> umap;
^
try2.cpp:11:5: error: 'umap' was not declared in this scope
umap["GeeksforGeeks"] = 10;
^
try2.cpp:15:15: error: 'x' does not name a type
for (auto x : umap)
^
try2.cpp:19:5: error: expected ';' before 'return'
return 0;
^
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ';' before 'return'
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ')' before 'return'
The compiler told you exactly what was wrong. It usually will.
This file requires compiler and library support for the ISO C++ 2011 standard. This
support is currently experimental, and must be enabled with the -std=c++11 or
-std=gnu++11 compiler options.
You just have to compile with the proper flag, -std=c++11. I don't know if you are version-matching against what graders use or what, but there are very few good reasons to be on a minGW compiler where support for an 8 year old standard is still considered experimental.
You can see that it works as expected here: https://godbolt.org/z/JQxL00
If you remove the -std=c++11 flag, it will fail to compile and give you the same error message.
You might also notice that I altered the includes to only include what I use. This results in a much faster compile time, smaller executable, and an easier to understand piece of code (Since it is plain to see what standard features are being used). You also avoid polluting your namespace.

Why this C++ program behave differently on different compilers? [duplicate]

This question already has answers here:
printf with std::string?
(9 answers)
Closed 7 years ago.
I was reading this. That question contains following program.
#include <iostream>
#include <cstdio>
#include <string>
int main()
{
using namespace std;
string myString = "Press ENTER to quit program!";
cout << "Come up and C++ me some time." << endl;
printf("Follow this command: %s", myString);
cin.get();
return 0;
}
I tried it on g++ 4.8.1 & it fails in compilation. g++ 4.8.1 gives following diagnosis.
9 47 [Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'
9 47 [Warning] format '%s' expects argument of type 'char*', but argument 2 has type 'std::string {aka std::basic_string<char>}' [-Wformat=]
What does this error mean? Should this program compile successfully or not? Which compiler is correct (g++ or MSVS 2010) ? Why MSVS 2010 accepts this code? Is the code invokes undefined behavior when compiled on MSVS 2010?
Surprising: I tried it on ideone which uses g++ 5.0 & surprisingly it compiles & runs fine. (See live demo here.). g++ 5.2.0 gives warning when I compile this code. (See live demo here). Why it compiles fine on ideone but fails on g++ 4.8.1? g++ 4.8.2(gives same diagnosis as g++ 4.8.1, 4.9.0,4.9.1,4.9.2 (gives error not warning). g++ 5.1.0 gives warning but program still compiles & runs fine.
Why different versions of g++ behave differently when compiling above program? Is this bug in g++ ? Clang also rejects this code to compile in response to answer given by #TemplateRex
Clang errors out with "error: cannot pass non-trivial object of type 'string' (aka 'basic_string') to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs]" and suggests the fix "note: did you mean to call the c_str() method?"
#include <iostream>
#include <cstdio>
#include <string>
int main()
{
using namespace std;
string myString = "Press ENTER to quit program!";
cout << "Come up and C++ me some time." << endl;
printf("Follow this command: %s", myString.c_str());
cin.get();
}
and it seems to work.

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>.