‘cout’ does not name a type
The problem is that the code you have that does the printing is outside of any function. Statements that aren't declarations in C++ need to be inside a function. For example:
#include <iostream>
#include <cstring>
using namespace std;
struct Node{
char *name;
int age;
Node(char *n = "", int a = 0){
name = new char[strlen(n) + 1];
strcpy(name, n);
age = a;
}
};
int main() {
Node node1("Roger", 20), node2(node1);
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
strcpy(node2.name, "Wendy");
node2.name = 30;
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
}
Error, cout does not name a type
Is it possible you forgot to include iostream?
#include <iostream>
This has to go to the top of your code.
Also in order for cout to work without std:: in front of it you have to use the namespace:
using namespace std;
You cant write code outside a function. You need at least an entry point:
//includes and namespaced go here
int main()
{
//enter your code here
}
I strongly suggest you work through basic tutorials.
Your updated code has some errors:
system("CLS")
You forgot the ";" at the end
if (button == n)
You dont have a variable named "n". You probably meant to write:
if (button == 'n')
Also in line 41 and 42, you have "\"s in your string. Those characters have a special meaning. Write another "\" before them to fix those errors.
The error is here:
int main(button){
It should be
int main(){
Ok i fixed your code:
#include <iostream>
#include <stdlib.h>
using namespace std;
char button = 'a';
int main() {
cout << " \n";
cout << " ********* ******** * **** ******** \n";
cout << " * * * * * * * \n";
cout << " * * * * * * * \n";
cout << " * * * * * * ***** \n";
cout << " * * ********* * **** * \n";
cout << " * * * * * * * \n";
cout << " * * * * * * * \n";
cout << " ********* ******** * * * * ******** \n";
cout << " \n";
cin >> button;
if (button == 'n')
{
system("CLS");
cout << " *** \n";
cout << " * ..* *** \n";
cout << " * u * * ..* *** . \n";
cout << " *** * u * * ..* *** \n";
cout << " * *** * u * * ..* \n";
cout << " *** * *** * u * \n";
cout << " * * * *** *** *** \n";
cout << " * * * * * * * * * * ***** O \n";
cout << " * * * * * * * * * ** ** *** * /|\\ \n";
cout << " * * * * * * * * /\\ \n";
}
else
{
system("CLS");
cout << "Invalid key enter n.";
}
}
Why do I get error 'cout' in namespace 'std' does not name a type when I use using cout = std::cout;?
using cout = std::cout;
refers to type alias declaration syntax. It's similar to typedef
; so you're trying to declare a type named cout
that refers to a previously defined type std::cout
. But std::cout
is not a type name, it's an object with type of std::ostream
.
As the error message said, it's just trying to tell you that std::cout
doesn't refer to a type name.
Multilevel Inheritance Error cout does not name a type
cout<<"\nEnter Patient ID\n";
is a statement that needs to be inside a function.
This, and your other statements, are floating somewhere in the class definition. This is not syntactically valid.
This is confusing the compiler and is issuing a somewhat cryptic error.
cout/cin does not name a type error
The body of the class declaration can only contain members, which can either be data or function declarations, and optionally access specifiers.
Wrap your code inside a function and then call that in main
by an object
class distanceFormula {
public:
int speed;
int time;
int distance;
void init()
{
cout << "What is the speed?" << endl;
cin >> speed;
cout << "How long did the action last?" << endl;
cin >> time;
distance = speed * time;
cout << "The distance traveled was " << distance << endl;
}
};
int main()
{
distanceFormula ao;
ao.init();
return 0;
};
cout does not name a type despite wrapped functions and namespace std
int main{
That should be
int main() {
Otherwise the compiler thinks you're trying to define an integer variable called main
, not a function, and will get very confused by the code that follows.
Also, compute_roots
never initializes its local variable x
before using its value, so that can't work:
double* x;
// ...
return x;
Another problem:
else if (d=0){
should probably be d == 0
(=
is for assignment, not comparison).
Conditional compilation, error: 'cout' does not name a type
Your cout
statements have to be in a function. Of the top of my head the only things that can be in global scope are global variable declarations and preprocessor directives. That means the #define
or #ifdef
alone would be fine, all the cout
stuff won't be.
#include <iostream>
#define PRINT_JOE
int main()
{
#ifdef PRINT_JOE
std::cout << "Joe" << std::endl;
#endif
#ifdef PRINT_BOB
std::cout << "Bob" << std::endl;
#endif
}
Also it's std::cout
and std::endl
Related Topics
Multiple Implicit Conversions on Custom Types Not Allowed
Do These Members Have Unspecified Ordering
Disable Eclipse's Error Discovery. (Codan False Positives)
Distinguish Between Single and Double Click Events in Qt
Are Class Members Guaranteed to Be Contiguous in Memory
What Operators Do I Have to Overload to See All Operations When Passing an Object to a Function
C++ Constructor/Destructor Inheritance
Initializing a C++ Std::Istringstream from an in Memory Buffer
How to Know If a Type Is a Specialization of Std::Vector
Multiple Inheritance from Two Derived Classes
C++ Fatal Error Lnk1120: 1 Unresolved Externals
C++ Overloaded Function as Template Argument
How to Make Gcc Warn on Passing Too-Wide Types to Functions