0

I'm trying to figure out why VS2012 doesn't show the function multi from functions.cpp @ main.cpp. in order to get it displayed in main.cpp I have to type int multi(); in main.cpp.

Here's 2 screenshots, the first screenshots is with int multi();in main.cpp

the second one is without. The code runs fine in 2 of the cases, but when I want to navigate to multi function in main.cpp file, I can't do it if I don't type int multi(); @ main.cpp

Can someone please explain me what I'm doing wrong ?

Thank you.

Example image 1

enter image description here

Example image 2

enter image description here

Here's my code

Main.cpp

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

using namespace std;
int plus();

int main()
{

cout << "Eneter a number you want to multiply" << endl;
cout << multi() <<endl;
cout << randomNumber << endl;

system("pause");
return 0;
}

Header.h

#ifndef _HEADER_
#define _HEADER_

#include <iostream>

int randomNumber = 4;
int multi ();

#endif

functions.cpp

#ifndef _HEADER_
#define _HEADER_

#include <iostream>

using namespace std;

int multi()
{
    int x;
    cin >> x;
    return(x=x+x);
}
#endif
Kirk
  • 16,182
  • 20
  • 80
  • 112
alentor
  • 27
  • 3
  • 12

3 Answers3

0

It's because that dropdown list only shows names of items that you declare/define in the current file. That's the intended behavior, because its purpose is navigation within the current file, and showing names of items in other files would take you to different files (plus it would show you things in headers like iostream, which you are not really interested in).

Other features of IntelliSense do show you the "multi" function, like typing "::":

enter image description here

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • Thank you! If I understand it right, if I'm using an external file to declare/define my functions, I'll still have to do a forward declare/define my functions in the file where I'm going to call/use these functions. correct? – alentor Mar 05 '13 at 04:03
  • No! You just have to include the header where they're declared/defined. The fact that the dropdown list at the top doesn't show them doesn't mean they're not available. And like I said, other features of IntelliSense do show them. – user1610015 Mar 05 '13 at 05:03
  • Thank you! You helped me to understand the programing language a bit more and the tool. Thank you. – alentor Mar 05 '13 at 06:13
-1

You can use only with the header file, and do that like this:

#ifndef _HEADER_H
#define _HEADER_H

#include <iostream>
using namespace std;

int randomNumber = 4;

int multi()
{
    int x = 1;
    cin >> x;
    return(x=x+x);
}

#endif

and with this way you don't need declare the function. what you tried to do is create a 'class' file ? better to you its just click right button on your project > Add > Class and you can see how it works.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ido Hadar
  • 179
  • 3
  • 5
  • 13
-1

Include guards, the #ifndef _HEADER_, #define _HEADER_, #endif lines don't belong in cpp files under normal circumstances. You should remove them from functions.cpp.

It may not cause you issues in a small program, but header.h and _HEADER_ are poor choices for names. You shouldn't use an underscore followed by a capital letter. More info here: What are the rules about using an underscore in a C++ identifier?

Community
  • 1
  • 1
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35