0

I have a simple program I made for class and I found an example of a program using a

.h foo.cpp and main.cpp

I entered that program and got it to compile fine but when I move split mine up I cannot get it to compile.

What is the typical process for running methods in main from the other .cpp files?

Here is my current program that runs,

main.cpp

#include <iostream>
#include "cars.h" 

std::ostream& operator<<(std::ostream& s, const cars& c) {
    return s << c.make << ' ' << c.model  << ' ' <<  c.col << ' ' << c.wheels;
}

int main() {

    cars c("Audi", "A4", "Black", "4");
    cars q(c);

    std::cout << c << '\n';

    std::cout << q << '\n';

    return 0;
}

If I wanted to create a cars.cpp file and split it up so I just cout in main method what rules would I follow here?

For instance should I override in the main.cpp or should I move that to cars.cpp?

I could obviously copy the other program I found but I want to understand this.

Also no this is not the assignment. I finished it I want to know how to do this because I am a Superhero who happens to love being ahead of everyone else.

wuno
  • 9,547
  • 19
  • 96
  • 180
  • What does "using a `.h foo.cpp and main.cpp`" mean? Are you saying you have a file whose name is just `.h`? – Keith Thompson Sep 03 '14 at 20:50
  • possible duplicate of [C++ Header Files, Code Separation](http://stackoverflow.com/questions/280033/c-header-files-code-separation) – bolov Sep 03 '14 at 20:51
  • No I am illustrating an example of what I am trying to figure out. In my program I posted you can see clearly I am including a cars.h file So if I have a class would it go in the cars.cpp then main.cpp inherit that? and how do I get main.cpp to inherit cars.cpp – wuno Sep 03 '14 at 20:52
  • Can you be more specific about the problem you're running into or how you want to change the setup you have? – aschepler Sep 03 '14 at 20:58

2 Answers2

2

Add

extern std::ostream& operator<<(std::ostream& s, const cars& c);

in cars.h.

Move the implementation,

std::ostream& operator<<(std::ostream& s, const cars& c) {
    return s << c.make << ' ' << c.model  << ' ' <<  c.col << ' ' << c.wheels;
}

to cars.cpp.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • ok so I should override in cars.cpp? Is it better to use extern then friend? I was using the same line but `friend` – wuno Sep 03 '14 at 20:54
  • Q1: Yes. Q2: The `extern` line provides a declaration of the function. In C++ every function has to be declared before it can be used. In this case, without the declaration, `cout << c` will fail to compile. – R Sahu Sep 03 '14 at 21:00
  • @NDiaz it adds an optional, but unnecessary, decl-part (`extern`) to an otherwise perfectly valid [**function prototype**](http://en.wikipedia.org/wiki/Function_prototype), the provision of which is the real meat of this answer (and is correct +1). Add `extern` at your leisure, but the prototype itself is crucial for global access to anyone wanting to use that operator and is including that header file to do so. Think of it as an announcement that said-function exists somewhere in the code to be linked later. – WhozCraig Sep 03 '14 at 21:01
  • Cool thanks. I was using `friend` but extern doesn't work because it is private. I will go read about it. Thanks a ton everyone. On another not I guess what I need to be reading about is `inheritance`? – wuno Sep 03 '14 at 21:05
  • @WhozCraig Oh my bad, I guess thats what you meant in the link you provided. Cool thanks man. – wuno Sep 03 '14 at 21:06
  • @NDiaz that operator should not be a member function. It should be a "free" function. outside of any class definition. Figured I'd tell you now rather than you struggling to figure it out later. If it accesses non-public members of `cars` it should be friended to the class, but should remain in the global namespace regardless (both the prototype and the implementation). – WhozCraig Sep 03 '14 at 21:08
  • @NDiaz you can put it anywhere you want. cars.cpp would be the logical choice, but it should not be a member of the *class*. It should be treated like a simple global function. – WhozCraig Sep 03 '14 at 21:10
1
  1. Move the ostream operator in cars.cpp
  2. Add a declaration for it in cars.h
  3. Include iostream in cars.h

I assume cars.cpp includes cars.h already?

ventsyv
  • 3,316
  • 3
  • 27
  • 49