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;
}
Related
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
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.
I have a question about testing user-defined header file.
So, here is the example.
#include <iostream>
using namesapce std;
#include "header.h"
#include "header.h" // I wonder the reason why I need to write this down two time for testing ifndef test.
int main()
{
cout << "Hello World!" << endl;
cin.get();
}
I learned that I need to write down user-defined header file's name in driver.cpp two times. But, I cannot understand the reason why I need to do that.
And, here is the second question.
#include <iostream>
#include <fstream>
using namesapce std;
int main()
{
fstream fin;
fin.open("info.txt");
if(!fin.good()) throw "I/O Error\n"; // I want to know what throw exactly do.
else cout << "Hello World!\n";
cin.get();
}
So, my question is the function of throw.
I realized that if I use throw instead of cout, my compiler would be terminated.
I want to know what does throw exactly do.
Since I am new to be here, I might have made some mistake about formatting and some rules, so feel free to point them out if I made it.
Thank you !
To your first question imagine following example:
MyHelper.cpp
int sum( int first , int second )
{
return first + second;
}
test.cpp
#include<iostream>
#include"MyHelper.cpp"
#include"MyHelper.cpp"
int main()
{
std::cout << sum(2,4) << std::endl;
}
Now as you know the precompiler replaces the line #include"MyHelper.cpp" with the content of the corresponding file.
// here comes the code from iostream header
// ...
int sum( int first , int second )
{
return first + second;
}
int sum( int first , int second )
{
return first + second;
}
int main()
{
std::cout << sum(2,4) << std::endl;
}
This is the code which the compiler receives. As You see the symbol sum is now defined more than once. So the compiler will exit with an error like:
t.c: In function ‘int sum(int, int)’:
t.c:7:9: error: redefinition of ‘int sum(int, int)’
int sum( int first , int second )
^
t.c:3:9: note: ‘int sum(int, int)’ previously defined here
int sum( int first , int second )
^
In other words if something with your includeguard is wrong then the compiler will report it to you with this error.
Second question:
throws is used to throw exceptions. I think there are thousands of tutorials to exceptions. simply ask your search engine of choice for "C++ exceptions tutorial" and follow one or two of them. Like following one for example:
http://www.learncpp.com/cpp-tutorial/152-basic-exception-handling/
(scroll a bit down to "A more realistic example" if you're only interested in use cases)
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 using Visual Studio 2010, and I'm wondering why I'm getting an error.
The error is: cout is undefined
#include<iostream>
#include<stdio.h>
#include<conio.h>
int main()
{
cout<<"Why am I not working ??";
printf("My Name is Khan and I'm not a terrorist.");
return 0;
}
cout is in the std namespace. You either need to declare that you are using the std namespace by adding the following to your code (it is generally put just after includes), though this is generally considered bad practise for non-trivial code:
using namespace std;
Or you can qualify cout every time it is used (this is generally preferred):
std::cout << "Hello, World!" << std::endl;
cout is a global object that lives in the std namespace. You have to fully qualify the name:
std::cout << "Hello";
// ^^^^^
If you really want to omit the qualification, you could have a using declaration in main() before you use the unqualified name cout (in general, avoid putting using declarations at global namespace scope):
// ...
int main()
{
using std::cout;
// ^^^^^^^^^^^^^^^^
cout << "Why I'm not working ??";
// ...
}
Add the following before int main:
using namespace std;