-4

I'm just trying to test splitting code into multiple files.

I have:

//testMultiple.cpp

#include <string>
#include <iostream>
#include "testInclude.cpp"

int main(){
    std::cout << "hi";
}

and

//testInclude.cpp

class testClass{
    public:
        string x;
};

This is giving testInclude.cpp:3:9: error: ‘string’ does not name a type

I thought since it was including before it included the testInclude.cpp, string would be defined for use in testInclude.cpp.

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
David Bandel
  • 252
  • 3
  • 19
  • 1
    Why are you including a `.cpp` file, instead of linking it with your `testMultiple.cpp`?? That's very unusual, and will break most buildsystems and IDE's. – πάντα ῥεῖ Jul 28 '15 at 08:56
  • Because as far as I know, if you want to split your program into multiple files, you `#include` them – David Bandel Jul 28 '15 at 08:57
  • No, that's wrong. You split them into a header file containing the class declarations, and a `.cpp` file containing the corresponding definitions. Other translation units (`.cpp` files) use `#include` to get the declarations from the header. – πάντα ῥεῖ Jul 28 '15 at 09:00
  • @DavidBandel Maybe you should first read a few question, such as [this one](http://stackoverflow.com/questions/1945846/what-should-go-into-an-h-file). – undu Jul 28 '15 at 09:01
  • @DavidBandel _"I had no idea it was a useless inclusion and string was actually defined in iostream."_No, you can't rely on this. If you use `std::string` use `#include `. – πάντα ῥεῖ Jul 28 '15 at 09:03
  • Yes that says to put them in .h files. So I changed it to testInclude.h and that didn't fix anything – David Bandel Jul 28 '15 at 09:05
  • Ok I figured out what I was doing wrong. I got it to work with #include in the header file like you said. So I'd just like to understand why I can't include vector in the main cpp, before I include the header file and the header file have access to vector. Does this mean if I have a bunch of code across a bunch of files, I have to include all my libraries and header files in each one? – David Bandel Jul 28 '15 at 09:27
  • I had this idea, from all the examples and tutorials I went over that std was an iostream thing. I understand what a namespace is better now. I had the naive idea that needing to preface something with std:: without a `using namespace std` meant I was using something defined in iostream. – David Bandel Jul 28 '15 at 09:43

3 Answers3

2

You need to use std::string instead of string.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Marcel
  • 203
  • 2
  • 8
1

Use

class testClass{
    public:
        std::string x;
};
Nishant
  • 1,635
  • 1
  • 11
  • 24
  • I already have `#include ` See my first file And I'm not trying to use the `` string. I'm trying to use the `` string. I simply want to split my code into multiple files. – David Bandel Jul 28 '15 at 08:55
  • @DavidBandel What do you mean _"I'm trying to use the `` string"_? I don't get what you want, the answer is correct. It's the way you try to organize your code that's wrong. – πάντα ῥεῖ Jul 28 '15 at 08:58
  • I am trying to put some code in another file, then include it, so that my code is split across multiple files. That's all. – David Bandel Jul 28 '15 at 09:02
  • Ok thanks. It worked your way. I just had a misunderstanding of where vector gets defined. – David Bandel Jul 28 '15 at 09:38
1

You're including a cpp file, not a hpp file. Common practice is to include header (h/hpp) files, not implementation (c/cpp) files.

If you only compile testMultiple.cpp, this should work. If the compiler is compiling testInclude.cpp separately, it will not see the `#include

Try renaming testInclude.cpp to testInclude.hpp and ensure it is not being compiled.

Here's an example:

///// testInclude.h
#include <vector>
class testClass{
    public:
         std::vector<int> x; // vector is in std namespace
};

///// testMultiple.cpp
// #include <vector> - gets this through testInclude.h
#include "testInclude.h"

int main(){
}
Sigve Kolbeinson
  • 1,133
  • 1
  • 7
  • 16