2

I'm having a weird problem compiling a function when I try using multiple files. I've boiled it down to this simple example: suppose I want to find the sum of a vector of integers. If I try compiling the following code, it works as expected:

#include <vector> #include <iostream> using namespace std; int VectorSum (const vector<int>& values) { int S = 0; for (int t=0; t < values.size(); t++) { S += values[t]; } return S; } int main() { vector<int> values; values.push_back(-100); values.push_back(75); values.push_back(75); cout << "Total = " << VectorSum(values) << endl << endl; cin.ignore(1, '\n'); return 0; } 

However, if I try using a header file, it crashes on my (error C4430 when compiling on VS 2010 for Windows XP). Here's the code for the other approach:

the header:

/* VectorSum.h */ #pragma once #include <vector> int VectorSum (const vector<int>& values); 

the source:

/* VectorSum.cpp */ #include "VectorSum.h" #include <vector> int VectorSum (const vector<int>& values) { int S = 0; for (int t=0; t < values.size(); t++) { S += values[t]; } return S; } 

the implementation:

/* Main.cpp */ #include "VectorSum.h" #include <vector> #include <iostream> using namespace std; int main() { vector<int> values; values.push_back(-100); values.push_back(75); values.push_back(75); cout << "Total = " << VectorSum(values) << endl << endl; cin.ignore(1, '\n'); return 0; } 

As you can see, the code for the function in VectorSum.cpp is identical to the code in my first .cpp file, so the problem must be in the header. Any ideas what I'm doing wrong?

4
  • 2
    You should use std::accumulate (values.begin (), values.end (), 0);... Commented Aug 4, 2011 at 20:09
  • I am pretty sure that this is not what is causing the error, but if you include <vector> in the .h, you do not need to include it again in the .cpp that include that .h. Could you try it that way? Commented Aug 4, 2011 at 20:09
  • @CCC C4430 is a compilation error, isn't it? Please put the full error text and tell us on which line the error is. Commented Aug 4, 2011 at 20:12
  • Armen wins. Thanks everyone! Commented Aug 4, 2011 at 20:20

5 Answers 5

3
#pragma once #include <vector> int VectorSum (const std::vector<int>& values); ^^^^^ 

See the MSDN page for C4430. It is issued in case when a declaration is missing a type or the type is unknown. In your case vector is an unknown type due to unqualified name lookup rules.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, that was fast. I'm kind of feeling like an idiot for missing that -- nice to have another set of eyes take a look. Thanks!
2

It's an issue with the std namespace.

Change the declaration in the header to:

int VectorSum (const std::vector<int>& values); 

And make sure there's either a using namespace std; in the .cpp file(s) (like your first example) or that you use the std namespace appropriately when calling/defining the function. For example, you'd need to do one of these things in your VectorSum.cpp file.


As an aside, please don't add a

using namespace std; 

statement to the header file. That will force the namespace to be brought into scope for all users of the header (even if it's indirectly included, so it might not be obvious), which might not fit their wants or needs.

Comments

0

The problem is indeed in the header file. You forgot to add

using namespace std; 

to the header and thus the compiler doesn't know what vector means.

Corrected header:

/* VectorSum.h */ #pragma once #include <vector> using namespace std; // This was missing int VectorSum (const vector<int>& values); // Now OK, the compiler knows vector 

5 Comments

No, don't use using namespace std; in a header! Just use std::vector.
-1, putting using namespace in the global scope in some header is just terrible.
I'd say don't use using namespace std; at all but that's not what this question is about. The OP seems to be using this directive everywhere anyway.
Because he never uses a header? (except for the malformed one)
This would pull everything in the std namespace into the global namespace of anything that includes VectorSum.h. That's evil.
0

You have to specify the namespace for the vector in VectorSum.h:

int VectorSum (const std::vector<int>& values); 

Comments

0

Add a using namespace std

/* VectorSum.h */ #pragma once #include <vector> <-------- //using namespace std; int VectorSum (const std::vector<int>& values); 

Try to avoid using namespace in header files to avoid name conflicts.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.