This question already has answers here:
Why statements cannot appear at namespace scope?
(3 answers)
Closed 5 years ago.
I don't know if I am just using the wrong keywords.. but I can't find an answer on google. Not can I wrap my feeble mind around my error.
This is a simple demonstration of the error:
#include <iostream>
//std::cout << "hello";
int main()
{
std::cout << "hello";
return 0;
}
Upon compiling/running this I receive this error:
main.cpp:3:6: error: 'cout' in namespace 'std' does not name a type
However, if I remove the first cout line, and just allow the program to execute the one inside of the main function, it works just fine.
Anyone got any ideas?
You cannot run code outside functions in C++. The compiler only expect variable declarations outside functions and, thus, expects std::cout to be a type, which it is not.
Related
This question already has answers here:
Function call missing argument list warning
(2 answers)
Closed 2 years ago.
I'm learning C++ online and working with Visual Studio Community 2019 (version 16.7.2). Sometimes it displays errors or weird results despite using the exact same code as the one the tutor used. Take the code below for instance. It should print out to the console but it doesn't. Instead, it shows an empty console when I run it on my end. What could be the problem? The teacher is also using Visual Studio on a Windows machine.
#include <iostream>
using namespace std;
void printSomething();
int main()
{
printSomething;
return 0;
}
void printSomething()
{
cout << "Hey! Over here." << endl;
}
Because you are only "mentioning" it here printSomething;.
To actually call it try the appropriate operator () as in printSomething();.
This question already has answers here:
Why GCC allows calling this function without using its namespace first? [duplicate]
(1 answer)
Why does C++ parameter scope affect function lookup within a namespace?
(3 answers)
What is "Argument-Dependent Lookup" (aka ADL, or "Koenig Lookup")?
(4 answers)
Closed 4 years ago.
Case 1:
#include <iostream>
int main()
{
std::cout<<"Hello World"<<endl;
return 0;
}
Compiler give an error because endl need namespace std like std::endl.
case 2:
#include <iostream>
int main()
{
endl(std::cout);
return 0;
}
But, In second case without namespace std, endl working fine. Demo.
Why does endl working fine without namespace std?
This question already has an answer here:
Does not name a type
(1 answer)
Closed 5 years ago.
very easy but frustrating question from me.
#include <vector>
#include <iostream>
using namespace std;
vector <int> queue;
queue.push_back(2);
int main(){
cout << queue[0] <<endl;
}
compiled with g++ -std=c++11 Cpp_test.cpp, return error
Cpp_test.cpp:51:1: error: ‘queue’ does not name a type
queue.push_back(2);
Can anyone help? Thanks a lot!
queue.push_back(2); should go in main.
To clarify, you cant just place code arbitrarily and have it executed. Declarations are fine outside of main, but that's not a declaration.
Jay is correct. However, since you're using C++11, you can keep your "initialisation" near the declaration by actually making it an initialisation:
vector<int> queue = {2};
(live demo)
By the way, std::vector is a strange choice for a queue.
This question already has answers here:
What are the rules about using an underscore in a C++ identifier?
(5 answers)
Closed 7 years ago.
I tried to define a global array, named _end, of size ~1000 in C/C++, but I got a segmentation fault even when I simply iterated it. Is the name "_end" very special in C/C++ that causes such problem? Or this can be a very serious bug... (The code is attached below, and it breaks in g++ 4.3.2, 4.5.2, 4.9.2, etc.)
#include <iostream>
using namespace std;
int _end[1111];
int main() {
for (int i=0; i<1111; i++) {
cout << i << endl;
_end[i]++;
}
return 0;
}
You can see the result at https://ideone.com/XAcUeZ.
See here also for the C compiler.
Names which start with an underscore (or two) are reserved for the compiler. This is official C++ standard. Use at own risk.
This question already has an answer here:
Error when i do not include string header file in c++
(1 answer)
Closed 9 years ago.
I can use cout to print a normal variable just fine, but whenever I try to print a function variable(in this case, string input) the compiler shoots out the error:
C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'std::string' (or there is no acceptable conversion)
I have posted my code below. Is there anything that I am doing wrong?(I am using the C++ version of Eclipse)
Here's my code so far:
#include <iostream>
using namespace std;
void println(string text) {
cout << text << endl;
}
int main() {
int test = 5;
cout << test;
return 0;
}
If you want to use std::string, you need to include <string>! The type may be declared or even defined because it is used by the IOStream classes but the implementation may choose not to include the entire header. In your code it seems you got a definition of std::string but not the declaration of its output operator.
Operator << for class std::basic_string are declared in header <string>. So you need to include it in your program that the compiler will know about it.