Why is the following code illegal?
#include <iostream>
using namespace std;
namespace what {
void print(int count) {
cout << count << endl;
}
}
void what::print(const string& str) {
cout << str << endl;
}
int main() {
what::print(1);
what::print("aa");
return 0;
}
The error I get when compiling with clang and -std=c++14 is
error: out-of-line definition of 'print' does not match any declaration in namespace 'what'
I know the fix to the problem but I am wondering why the compiler thinks that I am trying to define the function (print) instead of overload it.
The reason it is not working for you is because the syntax
void what::print(const string& str)
is basically saying
inside the what namespace, define the print function here
If you want to define a function outside of its namespace, you must declare it in the namespace beforehand.
§13.1 of the standard states, "When two or more different declarations are specified for a single name in the same scope, that name is said
to be overloaded."
Overloads of a function must be in the same scope of each other. It is just how the language works.
Related
Something interesting and wrong about my code.
#include <iostream>
void func(){
using namespace std;
}
main(){
func(); //Here the function will introduce the (using namespace std declaration) in the code
cout << "Hello World!";
return (0);
}
When compiled the error message is shown:
atizva#atizva:~/Documents/C++/Programs$ g++ -o func function_call.cpp
function_call.cpp: In function ‘int main()’:
function_call.cpp:7:2: error: ‘cout’ was not declared in this scope
cout << "Hello World!";
^~~~
function_call.cpp:7:2: note: suggested alternative:
In file included from function_call.cpp:1:0:
/usr/include/c++/7/iostream:61:18: note: ‘std::cout’
extern ostream cout; /// Linked to standard output
^~~~
I don't understand why the function 'func()' doesn't call the tag: 'using namespace std' appropriately.
To fix this, you would have to move using namespace std; outside of func(). The reason this fails in your current code is that using declaration only applies within the scope that it is called (in this case func()). So once you exit func(), you lose the effects of using namespace std;
You assume that namespaces are enabled at runtime, but namespaces are only meaningful at compile time. What you are doing is to limit the use of std to the scope of the function func(). That is, it allows you to type
cout
inside that function, but not elsewhere.
Why am I getting this error from the compiler about the function not taking 0 arguments? Is is because I declare the function after it has been called?
// HelloWorld.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
cout << "Game over!\n";
swap();
system("pause");
return 0;
}
int swap()
{
int on = 1;
int off = 0;
int temp = on;
on = off;
off = temp;
return 0;
}
Is is because I declare the function after it has been called?
Yes.
By the time the compiler sees the call to swap(), it doesn't know about your function yet. You'd normally get an error along the lines of “call to undeclared function” in this case, were it not for std::swap (which takes two arguments) that you've pulled into your name-space by the using namespace std directive.
In order to fix: Move the definition of swap above main (as a function definition is always also a function declaration) or leave it where it is an put a dedicated declaration
int swap();
above main. I'd also get rid of the using namespace std; as it, as you can see, might do you more harm than good and instead prefix all standard-library types and functions explicitly with std::. But that's not mandatory and also not the root cause of your current issue.
Try defining your function on top of main or Just declare on top of main.It now calls swap from .net library
So far I have typed this,
#include iostream
using namespace std;
void PrintIntro();
I want to assign a actual text now to the PrintIntro function so that in my main program I can just type
PrintIntro() and when the program runs the text assign to the function will show.
So far I have tried this after "void PrintIntro();"
{ /*PrintIntro*/
cout <<
"==================================================" << endl;
cout <<
"Welcome to the Math Practice Program!!!!!" << endl;
cout <<
"This Program will help you practice elementary math" << endl;
cout <<
"==================================================" << endl;
/*PrintIntro*/
}
But then I get the error under the "{" symbol indicating that it is "expecting a declaration." I have been searching through notes and messing with this all day and I cannot figure it out. Any help would be appreciated. I am using MS Visual studio Express 2013.
Adding the semicolon after void PrintIntro() tells the compiler that there is a function called PrintIntro that takes no arguments and returns void, and that you are defining it later. This is called a forward declaration. Chances are this is what's happening:
void PrintIntro();
//Compiler: okay, that was a forward declaration
{
//Compiler: wth is this stuff?
}
You want this to happen:
void PrintIntro();
//Compiler: okay, that was a forward declaration
void PrintIntro()
{
//Compiler: oh, this is the definition for that function you told me about earlier
}
Or you want to do it without the forward declaration:
void PrintIntro() //no ';'
{
//Compiler: declaration and function body all in one part - simple!
}
You should also change #include iostream to #include <iostream>
Remove the ; after you declare you function like:
void PrintIntro()
When declaring a function, you do not need to end the function declaration line with a semicolon.
So your function should look like
void PrintIntro(){
blahblahblah...
}
You need to remove the semi column at the end of the declaration
void PrintIntro();
Must be like this
void PrintIntro(){
}
A couple of errors, most of them in the syntax I think. Note that the line #include iostream should actually be #include <iostream> and the ; is missing after void PrintIntro().
Like so
#include <iostream>
using namespace std;
void PrintIntro(){
cout << "Hello world" << endl;
}
This is a question regarding the default global namespace in C++. I have the following code that compiles and runs properly using g++ clang-500.2.79.
#include <string>
#include <iostream>
using std::string;
using std::endl;
using std::cout;
bool is_palindrome(const string& str){
return equal(str.begin(), str.end(), str.rbegin());
}
int main(){
cout << "Hello is a palindrome: " << is_palindrome("Hello") << endl;
cout << "madam is a palindrome: " << is_palindrome("madam") << endl;
return 0;
}
My questions is, why does this code compile properly? I forgot to put #include <algorithm> and using std::equal at the beginning of my file. So the expected behaviour is for the compiler to complain.
The example at http://en.cppreference.com/w/cpp/algorithm/equal confirms that I should be using std::equal.
To investigate this further, I tried to track down exactly which version of the equal() function was being called. Being a relative newbie to C++ I don't know exactly how to do this either. I tried,
cout << "The function is: " << equal << endl;
Which generated a compiler error with some interesting information:
/usr/include/c++/4.2.1/bits/stl_algobase.h:771:5:
note: 'std::equal' declared here
Try as I might, I can't find information about stl_algobase (or more probably, I most likely don't understand what I've found). Is stl_algobase a set of functions that are automatically included in the global namespace?
A further questions is: What is the proper way to track (code or otherwise) down which function is being called when you are dealing with potentially overloaded or template functions in C++?
equal is in the std namespace. What you are seeing is argument dependent lookup (ADL). Because the arguments are in the std, the name lookup for equal considers that namespace too.
Here's a simplified example:
namespace foo
{
struct Bar {};
}
namespace foo
{
void bar(const Bar&) {}
void bar(int) {}
}
int main()
{
foo::Bar b;
foo::bar(b); // OK
bar(b); // ADL, OK
foo::bar(42); // OK
bar(42); // No ADL: error: 'bar' was not declared in this scope
}
I'm writing my first C++ application. But I get syntax error.
#include <iostream>
using namespace std;
int main() {
int result = get_num();
cout << "Result is " << result << endl;
system("pause");
return 0;
}
int get_num(void) {
return 1;
}
And compiler said me:
main.cpp(10): error C3861: 'get_num': identifier not found
Two options:
1) declare a prototype of get_num before main:
int get_num(void);
int main() {
}
2) move your definition of get_num before main.
One solution would be to add a forward declaration before main like so:
int get_num(void) ;
the other solution would be to put the definition of get_num before main and then you would not need a forward declaration.
Write int get_num(void); above the main() function.
C++ requires variables and functions to be declared above the current scope.
In C++ you need to declare all variables/functions that you want to use before using them. You're using getnum in main but you haven't declared it in the function. Writing int get_num(); outside main will declare this at a global scope. i.e. any function in that file would be able to use it. declaring get_num(); inside a function will enable you to use this function only inside that particular function.
You need to declare or define the function before using it in the main.
To declare just add
int get_num(void);
at the beginning of your code. If not define the entire function before main like so-
Try-
#include <iostream>
using namespace std;
int get_num(void) {
return 1;
}
int main() {
int result = get_num();
cout << "Result is " << result << endl;
system("pause");
return 0;
}