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.
Related
how do I compile a .c or .cpp file using GCC?
I need to include some standard libraries (fstream, string, iostream) - how do I do this?
For clarification, here:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include"ocgenerator.h";
#include"structures.h";
#include"util.h";
#include"util2.h";
using namespace std;
(my .h files are in the same directory as the src file)
If I use the command:
gcc src.cpp -o src.o
I get a lot of errors: memcpy, atoi, atol, strncmp, etc, ... "are not declared in this scope". What should I add to the command?
edit: Or is it a scope thing, and do I have to add std:: to all those functions?
memcpy and strncmp are declared in <cstring>, atoi and atol in <cstdlib>. Just include these headers to bring in their declarations.
Side notes :
No semicolon after preprocessor directives, including #include "".
No using namespace std;, especially not in headers !
(Why is "using namespace std" considered bad practice?)
Since you're building a C++ project, don't forget to link with the standard library via -lstdc++, or use g++ which forwards it to the linker for you.
Note that with GCC you don't have to prefix standard C functions with std:: (as they are also declared in the global namespace), but you should anyway for your code to be standard-compliant.
The cplusplus site helps out. If you search for a specific function, on the top you will find the necessary references for the code to compile:
http://www.cplusplus.com/reference/cstdlib/atol/
You can even check in the URL. In this case, you need 'cstdlib'.
Regarding the compiler, there are several available, g++ is a good option.
I also suggest creating a makefile for automating the building process, because it becomes an headache when your codebase grows.
For any library function you use in your code, read the man page for that function to see which header declares it, and #include that header in any source file that uses the function.
For example, man memcpy shows the memcpy(3) man page (the (3) refers to section 3 of the manual; use man 3 memcpy to specify the section).
NAME
memcpy - copy memory area
SYNOPSIS
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
...
memcpy is part of the C standard library, and is declared in <string.h>. For C++ you can use <string.h>, but it's probably better to use <cstring>, which puts the function name in the std namespace. In general, each standard C header <foo.h> is duplicated as a C++ header <cfoo>.
I get a lot of errors: memcpy, atoi, atol, strncmp, etc,
memcpy and strncmp are declared in <cstring>. atoi and atol are <cstdlib> (or in <string.h> and <stdlib.h> if you're programming in C rather than in C++).
And be sure to use the g++ command, not the gcc command, to compile C++ source. They both invoke the same compiler (gcc compiles C++ code if the source file name ends in .cpp), but the g++ command adds some options that are needed for C++.
When I try to compile c++ code, my include statements don't seem to be working. I haven't been c++ programming on my computer in a while, but this has never happened before.
I made a minimal test script, and upon compiling (g++ infile.cpp -o outfile.out) I get: "error: iostream: No such file or directory"
The same problem happens for vector, string, and I'm guessing other libraries.
Why isn't g++ finding the appropriate libraries?
#include <iostream>
int main() {
return 0;
}
Sounds like you somehow hosed your compiler. You'll need to reinstall it or something.
A normal install wouldn't need search paths and you shouldn't be including iostream.h even though it probably exists (gcc's non-h headers include the .h headers).
This of course assumes your compiler version isn't like 20 years old. If that's the case then iostream probably doesn't exist and iostream.h would be the correct header to include. But you've got a difficult life if that's the case and you'd have to be seriously resistant to change to have a compiler that old. My bet is that this just isn't the case.
Trying to brush up on my C++, I picked up a helper function I needed from a web search and tried it out before looking it up in the C++ reference:
int count_vowels(const std::string &input) {
return std::count_if(input.begin(), input.end(), is_vowel);
}
When I looked up more details on count_if(), I found that it's part of the <algorithm> library code (http://www.cplusplus.com/reference/algorithm/count_if/), which I had not included when I compiled and ran it. Why would the function work without the <algorithm> header? I have included <iostream>, <string> (obviously) and <sstream<>. And I'm using the compile flag -std=c++11 if that matters at all.
Also, if it works without the <algorithm> header, should I put that header in anyway for clarity's sake (or because other compilers wouldn't necessarily pick up the necessary function definition)?
It works because it's probably included indirectly via one of the other headers. It's not guaranteed though, and it might break on a different compiler, or a future version of the one you're using now.
Include all the headers you need directly.
If you are using Visual C++ then you can turn on show includes to see what files are included, via Project -> Settings -> C/C++ -> Advanced.
If using gcc then this explains the equivalent: /show include equivalent option in g++
You will then know where it is being included.
I'm trying to make something in Linux, but it complains that it can't find iostream.h. What do I need to install to get this file?
The correct name of this standard header is just iostream without an extension.
If your compiler still cannot find it, try the following:
find /usr/include -name iostream -type f -print
...and add it to your include path, following your compiler's documentation.
The header <iostream.h> is an antiquated header from before C++ became standardized as ISO C++ 1998 (it is from the C++ Annotated Reference Manual). The standard C++ header is <iostream>. There are some minor differences between the two, with the biggest difference being that <iostream> puts the included contents in namespace std, so you have to qualify cin, cout, endl, istream, etc. with "std::". As somewhat of a hack (it is a hack because header files should never contain "using" directives as they completely defeat the purpose of namespaces), you could define "iostream.h" as follows:
#ifndef HEADER_IOSTREAM_H
#define HEADER_IOSTREAM_H
#include <iostream>
using namespace std; // Beware, this completely defeats the whole point of
// having namespaces and could lead to name clashes; on the
// other hand, code that still includes <iostream.h> was
// probably created before namespaces, anyway.
#endif
While this is not exactly identical to the original antiquated header, this should be close enough for most purposes (i.e. there should be either nothing or very few things that you will have to fix).
I needed to compile partport on Debian and had problems (CentOS 4.5 worked fine). I did this without any success:
ln -s /usr/include/c++/4.5/iostream /usr/include/c++/4.5/iostream.h
I discovered that iostream.h was provided from C++, and I found it on CentOS 4.5.
So I copied the file iostream.h from CentOS 4.5 to Ubuntu 11.04 (Natty Narwhal), and it worked:
scp root#ip.centos-4.5:/usr/include/c++/3.3.4/backward/iostream.h /usr/include/c++/4.5/iostream.h
I have including problems in a C++ Project. I included math.h, but there are strange problems with my vector.h and my matrix.h header files. Am I allowed to call these files vector.h and matrix.h?
Two headers cannot have the same name.
By same name, the full path name is implied, so
#inlcude "testClass.h"
#include "heders/testClass.h" // OK, distinguishable
Visual studio prevents you from adding a header having a name that already exists in the project.
You should also check that your header is actually included in your project (or through your Makefile, build system etc). A quick check would be to cause a syntactic error in that header and see if it breaks the build
So to get back to your question, do you already have headers called vector.h and matrix.h? Cause that would be the only thing preventing you from naming new headers like that.
Keep in mind that headers accessed with #include <...> require their folder to be set as an include (external) directory so qualifying up to that path won't work
In theory I don't know of anything to prohibit doing so.
I'd consider vector.h close enough to <vector> that using it would be a poor idea.
I'm not exactly excited about matrix.h either, but at least it's not nearly so obviously a poor choice.
Of course, for any header you wrote yourself (rather than one provided by the tools you're using) you want to enclose the name in quotes, not angle brackets.
The rationale why C++ chose the unusual <vector> format without suffix is because the intent was to remain compatbile with existing C code which might very well have "vector.h". So the answer is yes, by design.