Error while compiling a C++ program with G++ [closed] - c++

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 7 years ago.
Improve this question
v This is main.cpp:
#include <stdio.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
To compile it, I go into cmd and type g++ main.cpp but it gives me 2 errors, saying that both cout and endl aren't declared. I can only imagine that it's because it can't find the namespace std or can't include <stdio.h>. How would I make this work?

You have to include iostream, not stdio.h to use std::cout and std::endl.

Related

Compile C++ Code Using The Terminal Directly Without Save File.cpp [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 8 months ago.
Improve this question
I need to compile C++ code directly in the terminal or CLI without saving the file
When is use the below way, It shows me an error.
gcc -x c - <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof
You are trying to compile a C++ program using a C compiler.
This works:
g++ '-xc++' - <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof
If you are trying to run a C++ program from a source,
this works:
g++ -xc++ - && ./a out <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof

Getting input in a String in cpp [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 8 months ago.
Improve this question
Hello I want to get some input in a string in cpp and I am getting and error. Here is the code:
#include <iostream>
int main() {
using namespace std;
string name;
cout << "Type your name:";
cin >> name;
cout << "Your name is: " << name;
return 0;
}
I am building the project and I get this error:
Test1.cpp:10:6: error: invalid operands to binary expression
It is this line: cout << "Type your name:";
What am I missing here ? It is the first time when I am using c++
You need to include header <string>
#include <string>
<iostream> does not include <string>. Hence, you also need to include <string> in order to use std::string.
#include <iostream> // for std::cout, std::cin
#include <string> // for std::string

Cannot print string with std::end [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 5 years ago.
Improve this question
I am having trouble with printing a string in C++.
I know there are lots of topics about this matter on SO, but most say to include <string>, <iostream> or namespace std. But I did all of that but still encounter the issue. Here is my code and the error.
#include <iostream>
#include <string>
using namespace std;
//...
void affiche_date(int annee, int nbjours) {
string mois;
if (nbjours>31) {
mois = "avril";
nbjours -= 31;
} else {
mois = "avril";
}
cout << "Date de Paques en " << annee << " : " << nbjours << " " << mois << end;
}
int main() {
int annee ( demander_annee() ) ;
int jour ( date_paques(annee) );
affiche_date(annee, jour);
}
Here is the error I get when I compile:
"error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘<unresolved overloaded function type>’)"
This error is coming from the line with the cout in the function I gave you.
I am using Geany on linux Ubuntu and using c++11.
Thanks for you help
std::end() is a function for getting an iterator to the end of a container.
You meant to use the std::endl stream manipulator instead.
Note: avoid using namespace std; in your actual code, either take advantage of using directives to bring in only what you need, or favor qualifying names with their namespaces, like I have here.

c++ : invalid operands to binary expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I don't know what's wrong here ?
It's just running errors !!!
#include <iostream>
using namespace std;
int main()
{
cout << string("hello world");
return 0;
}
Read more about C++. So read first Programming -- Principles and Practice Using C++.
Then read C++ reference documentation, notably the one about std::string-s.
You need to #include <string>
You should enable all warnings when compiling. If using GCC, compile with g++ -Wall -g
You don't need that string before the actual string:
#include <iostream>
using namespace std;
int main()
{
cout << "hello world";
return 0;
}
Or, alternatively, if you're trying to store a string:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "hello world";
cout << str;
return 0;
}

"{" missing function header (old style format list) - expected a declaration [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 7 years ago.
Improve this question
I'm really really new to C++ and this is my first program on Visual Studio 2015, It shows me 2 errors:
"{" missing function header (old style format list)
expected a declaration
#include "stdafx.h"
#include <iostream>
int main();
{
cout << "Hello World";
return 0;
}
int main() remove ; at the end.
#include "stdafx.h"
#include <iostream>
int main(); // this ';' is giving problem remove it.
{
std::cout << "Hello World"; // use std::cout
return 0;
}