Defining getch() function in c/c++ - c++

Why when I use getch() function in c++, I should include "conio.h" header file ... while in c it runs without include this file?
I'm using codeBlocks as my IDE.
I expect that I must include "conio.h" in c program also, and I tried to include "stdio.h" and "stdlib.h" in c++ program but there is no result.

Why when I use getch() function in c++, I should include "conio.h" header file ...
C++ requires that identifiers be declared before they are used.
… while in c it runs without include this file?
You are using an implementation of an old version of C in which a function call with an undeclared identifier defaulted to treating the identifier as one for a function with undeclared parameters with return type int.
I'm using codeBlocks as my IDE.
The IDE is irrelevant, except that there are some associations between some IDEs and some sets of development tools. The critical information is the name and version number of the compiler and the switches you are giving to it. Saying what IDE you are using is like saying what picture frame you are using in a question about a picture. The frame does not identify the picture, and the IDE does not identify the compiler you are using inside the IDE.

Related

visual studio does not give error if a header is missing

I am testing with a simple test program right now. It looks like below:
#include <iostream>
using namespace std;
int main() {
string s = "a b c d ";
remove(s.begin(),s.end(),' ');
}
When i build it with visual studio, it builds correctly and does not give any error. However if i try to build it with eclipse (mingw), it complains about the functions 'remove', as it should because the corresponding header is not included.
Is there a way to configure visual studio such that it will also complain and not auto-include headers or whatever fancy thing it is doing? I have already checked by disabling the option to use pre-compiled headers in visual studio project properties, and that doesn't help.
When you write a program that fails to include the proper headers, some toolchains may still just so happen to successfully build your program, because maybe their <iostream> happens to ultimately include the header you need (like <algorithm>).
That doesn't change the fact that your code is wrong. You're getting a build by chance.
You don't configure another toolchain to do that. You fix your code to include the correct headers.
So:
#include <string>
#include <algorithm>
The C++ Standard does not define, that a certain file needs to be included for the contained definitions to be able to be used.
It only defines in which files the specific functions are defined.
So if the specific implementation which You use includes everything through a file and You don't need to include anything else, than that is still allowed by the Standard.
So in one implementation, everything will compile, while in another errors will appear.
This is not controlled by the C++ Standard.
What You can do is file a bug to the implementors, and see if they agree that it's a bug. (In this case: https://github.com/microsoft/stl/issues)

how to disable only intellisense of system files(msvc,windows kits) in visual studio?

Visual studio Intellisense shows too many variables and functions. I found that most of functions are related to header in msvc/include or windows kits/include.
If you create an empty project and press 'a' in the main function, you can see the following picture.
press 'a' in main()
I did not declare #include anything, but intellisense shows system functions and variables due to automatically included system header files.
I want to use intellisense only for the headers or namespace I declared. I don't want to use that for system header files.
I'm afraid the answer is negative.
For now C++ Intellisense option doesn't support this behavior.
What you create seems to be a windows desktop app. In this way,windows content from #include <windows.h> is available when you use C++ Intellisense.
For a windows desktop app, the #include <windows.h> is necessary, and for C++ Intellisense, it won't recognize if some variables or functions come from windows.h. It will display all variables from the header files included in the .cpp file.(Both #include declared by you and #include windows.h declared in the framework)
In addition:Maybe you can go help menu=>send feedback=>provide a suggestion to post your idea for this.

Eclipse CDT Plugin not recognizing std namespace in certain files

I've just installed Eclipse and the CDT (C++) plugin, and I'm having the strangest problem.
From a fresh C++ Project, the automatically generated "Hello World!" program works just fine. However, upon creating a new C++ Class in the very same project, namespace std is not recognized. I invoke the namespace with the same syntax in both files, but the compiler is throwing this error -
"error: use of undeclared identifier 'std'"
I've restarted both Eclipse and my computer, but the problem persists. So far as I can tell, the properties of both files are exactly the same.
I suspected this had something to do with the tool chain, but it is the same in both of the files. I really can't find any difference between the two files, other than their size and date created.
Your program probably didn't #include any headers that define the namespace std. Please add the appropriate #include files that define the std namespace.

Declaring function returning string in C++

i am trying to declare function returning string in header file because i will call this from objective-c. basically this would work, isn't it?
std::string myFunction();
but it throws error message says "Expected ';' after top level declarator", searched a lot, everyones suggest put #include in header files, i tried that as well however it is not working, this time it throws another error message "'string' file not found".
have another function returns double and have no problem with it.
double doSomething(double a);
-
#include <string>
does not work it is throwing error message saying "'string' file not found" . have tried to create new project just in case mine could be damaged but it is not working should i put something in search paths etc?
at last i made it.
The solution: have changed "Compile Source As" settings to Objective-C++ under Build Settings / Apple LLVM Compiler 4.2 and it worked like a charm.
At the very least, you have to include the C++ standard library header string, where class std::string is declared (in actual fact, it is a typedef to a class template, but that is another matter).
#include <string>
std::string myFunction();
You should also make sure to use include guards in your own headers.
You missed the header file:
#include <string>
You have to remember that ObjC files are built upon C not C++, if you want to use a C++ file in Objective-C you need to change the extension of the ObjC file from .m to .mm to make it an ObjC++ file, or else it will be like trying to include C++ headers in C files.

istream and ostream problem - C++ [duplicate]

This question already has answers here:
C++ error: ‘string’ has not been declared
(3 answers)
Closed 10 months ago.
I am using two compilers g++ and Dev - C++. when I compile my program on Dev-C++ it compiles perfectly. but when i try to compile it on g++ it gives me two errors:
In file included from a2test.cpp:27:
----.h:25: error: 'ostream' has not been declared
----.h:26: error: 'istream' has not been declared
Can anyone tell me what can I do to solve this problem.
Thanks
Make sure you include fstream. Also, put "std::" before ostream or put "using namespace std" somewhere.
It would help if you posted the code, as right now I'm just guessing based on common mistakes.
I would guess you forgot to include fstream because different compilers may use different header files and it may be the case that g++ has a header file with
// iostream
#include <fstream>
While Dev-C++ may have
// iostream
// no include for fstream in this file
So you're accidentally importing the correct header file rather than doing it explicitly.
For header files, I just use this site when I forget which one.
ostream - C++ Reference
It seems you need to include ostream to get ostream. Probably the same thing for istream.
My psychic debugging skills indicate that the problem likely means that your call to g++ and the g++ Dev-CPP is using are different versions of gcc. One of the headers in the (presumably earlier) version included with Dev-CPP might #include a standard C++ header that it doesn't need to, which would allow headers which aren't strictly correct to compile.
Make sure you've actually #included <iostream>, or <istream> and <ostream>, or <iosfwd> -- some header which actually includes these types for you.
(Side Note: Please don't use Dev-CPP -- the project is pretty much dead, and the editor commits quite a few sins. Plus it isn't a good editor anyway. How about Code::Blocks or Visual Studio (both free) instead?)
dont know if this will help, but firstly, yuou should remember to omit the ".h" that some other compilers (MS-C++) use, but not ANSI/G++.so it should be just
#include <iostream>
Secondly, don't forget :
using namespace std;
3rdly, it's been a long time, but if I remember correctly, in g++, th istream and ostream functions are in the "std" library .. so you can do something like this :
using std::istream;
//later
istream::iostate state = ...
or alternatively, you can use them directly like this :
std::istream::iostate state = ...
Hopefully that'll give you some ideas.