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)
Related
i keep getting this error. i know this is a c++ 11 function but it still isnt working with code blocks c++ compiler. am i using this function correctly of is it a problem with the codeblocks compiler. i tried changing the compiler. using the "have g++ follow the c++11 iso standard" i still keep getting this error. or getting the "stoi() does not exist in the current scope" error
#include <iostream>
#include <string>
using namespace std;
int main()
{
int test = 34;
cout << stoi(test);
}
stoi means "String To Int". It will read an int from a std::string (or std::wstring). See also the reference.
You were probably looking for the reverse std::to_string (reference). But you don't need either, there is no need to convert to string before printing:
#include <iostream>
int main()
{
int test = 34;
std::cout << test;
}
stoi means string to int. So it takes a string as an input.
This should work:
string test = "34"; cout << stoi(test);
Visual Studio is going crazy on me recently, and gives me the error in the subject when all I did was a simple cout...
CODE:
// Lang.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main{
cout << "hi";
}
int main{
cout << "hi";
}
Due to the possibility of initialising objects in C++ with the {} syntax, your compiler probably interprets this code as an attempt to create a global int variable called main, initialised with the result of std::ostream::operator<<, a member function which returns a reference to the std::ostream itself.
It's as if you had written:
double some_variable { cout << "hi" }
Or:
double some_variable { cout }
std::ostream is actually std::basic_ostream<char, std::char_traits<char>>. And that type is not compatible with int.
The only thing which is strange is why the ; after "hi" does not immediately cause the compiler to stop trying; but you don't say which compiler version and which options you are using.
In any case, all of those facts finally result in the error message:
no suitable conversion function from “std::basic_ostream<char,
std::char_traits<char>>” to “int” exists
and in:
Also, the semicolon after "hi" is highlighted, and says "expected a }"
Solution: make main a function:
int main() {
cout << "hi";
}
I am relatively new to C++. I am trying to call a function using defined header. I have following 2 files(in addition to enter.h file):
// 1. main.cpp
#include "enter.h"
#include<iostream>
using namespace std;
int main()
{
int intdemo=enter();
cout << "The result is: " << intdemo<< endl;
}
// 2. enter.cpp
#include <iostream>
using namespace std;
int enter()
{
int thisisanumber;
cout<<"Please enter a number: ";
cin>>thisisanumber;
return thisisanumber;
}
I am getting the following error message "void value not ignored as it ought to be". and is pointed to the second line of the main function where value of the variable "intdemo" is assigned
Can anyone suggest how to fix this error? i have searched some of the similar posts here but cannot able to understand the problem. Since i am a beginner, any help would be appreciated.
Your header file probably declares the function enter() to return void (this was confirmed in the comments).
Changing this to match your function definition will solve the problem, as well as an unresolved external error you will most likely be getting as a side effect of this.
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;
}