-4

I am trying to compile this code, but I'm gettong an error:

#include <iostream>

main(){

    std::cout << add(5, 6);
}

int add(int a, int b){
    return a+b;
}

And I get the following error:

error: 'add' was not declared in this scope
  std::cout << add(5, 6);
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • 1
    Gotta have a forward declaration – bpgeck Aug 15 '15 at 23:39
  • as I commented on the post below, wow so functions don't see other function below them? that kinda sucks why aren't they fixing it. java developer here. – instructionSetQuestion Aug 15 '15 at 23:41
  • In C++ it's very very common to have a header file that has all of the function declerations and then just define those functions in your .cpp file – bpgeck Aug 15 '15 at 23:42
  • ok so i make another file called "functions.h" and include by "#include " ? – instructionSetQuestion Aug 15 '15 at 23:43
  • Since the functions.h is in the same directory you would do `#include "functions.h` – bpgeck Aug 15 '15 at 23:44
  • Yes. Then any file that includes functions.h can also use 'add'. – Cos314 Aug 15 '15 at 23:44
  • when do I use these sign: '<' – instructionSetQuestion Aug 15 '15 at 23:45
  • 2
    @instructionSetQuestion Also, functions require a return type. The function is `int main()`, not just `main()`. – PaulMcKenzie Aug 15 '15 at 23:45
  • With `""` surrounding the hesder file rather than with `<>` – bpgeck Aug 15 '15 at 23:46
  • ok but what about this: #include , it's using '<' not " " – instructionSetQuestion Aug 15 '15 at 23:47
  • 2
    Mate. You cannot learn C++ from a Stack Overflow comments thread. We are not going to have a chatroom style discussion here where you are taught every little detail of the language, one at a time. Here, read this! http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Good luck. – Lightness Races in Orbit Aug 15 '15 at 23:48
  • @instructionSetQuestion So when in comes to including libraries, your machine has environment variables that point to directories deep in your computer. The `<>` tells the compiler to look at this path variable – bpgeck Aug 15 '15 at 23:49
  • ok then last question. is it even worth learning?, i'm a java developer, should i go after something else? like iOS development? – instructionSetQuestion Aug 15 '15 at 23:50
  • 1
    @instructionSetQuestion Well C++ is one of the most used languages. Especially for something like the video game industry. Also ios development is currently done in objective C (although this may change to swift in the near future) and objective C has many similarities to C++. So you can stop learning but know these concepts likely won't go away when you stop haha – bpgeck Aug 15 '15 at 23:53
  • 1
    well these days web/app programming is in demand. C++ is for game/core development – instructionSetQuestion Aug 15 '15 at 23:54
  • Please google some basic C/C++ beginners guides.... – Qix - MONICA WAS MISTREATED Aug 15 '15 at 23:54
  • 1
    @instructionSetQuestion When it comes to CS I've found that you should just do what you most enjoy and find the industry that best aligns with those desires. Jobs will always be in demand in any facet of CS – bpgeck Aug 15 '15 at 23:55

1 Answers1

5

Before main() place:

int add(int a, int b);

Usually a function interface is placed in a header file and then included.

Cos314
  • 302
  • 1
  • 5
  • wow so functions don't see other function below them? that kinda sucks why aren't they fixing it. java developer here. – instructionSetQuestion Aug 15 '15 at 23:39
  • 1
    @instructionSetQuestion: It doesn't "suck", really. There are complex reasons for it and it works just fine. – Lightness Races in Orbit Aug 15 '15 at 23:49
  • 2
    @instructionSetQuestion All functions in Java are in a class. If you were to define those functions as member functions, you will find out that C++ does see the functions "below them". What you have are global, non-class functions, something that doesn't exist in Java AFAIK. For that, you must declare the function prototype (or the entire body of the function must appear) before you call the function. If you want this to be "fixed", then learn C programming, where you can call functions without declaring them (and lose the prototype checking). – PaulMcKenzie Aug 15 '15 at 23:49
  • We have it in java i think it's "extends" – instructionSetQuestion Aug 15 '15 at 23:52
  • Downvote, it has absolutely nothing to do with header files. – Qix - MONICA WAS MISTREATED Aug 15 '15 at 23:55
  • @Qix Yeah, it kinda does, because that's usually where you put forward declarations. – PC Luddite Aug 16 '15 at 01:02
  • No, forward declarations have nothing to do with headers. Headers are not required and without that piece of information the answer explains nothing. – Qix - MONICA WAS MISTREATED Aug 16 '15 at 01:02
  • @instructionSetQuestion You'll learn very quickly that a C++ compiler expects you to know what you're doing. It doesn't babysit you the same way Java does. This affords you more power, but you have to be more careful with what you write. IMO, it's a worthwhile trade-off. I hate being babysat. – PC Luddite Aug 16 '15 at 01:04
  • @Qix You could say that, but I'd disagree with you. That's pretty much what headers are for. Sure you could have one without the other, and you could write entire programs without any header files but still use forward declarations. I'm not sure why you think that information is so pertinent though. I think that it is worthwhile to say that this is what headers are for. – PC Luddite Aug 16 '15 at 01:11
  • @ThePcLuddite Headers are for declarations. That doesn't answer OP's question. – Qix - MONICA WAS MISTREATED Aug 16 '15 at 01:14
  • @Qix No your comment doesn't, but the above *does* answer the question. They said to place a declaration of the function *before* `main()`, and then suggested the use of a header file. – PC Luddite Aug 16 '15 at 01:15