passing adresses in functions

Heres sample code:

1
2
3
4
static vector<string> default_vs;
string default_s = string();

int blablabla(string bla = "", vector<string>& blabla = default_vs, string& bla = default_s)


heres what the header file would look like (without the include crap):

1
2
3
using namespace std;

int blablabla(string bla = "", vector<string>& blabla = default_vs, string& bla = default_s);


Here is the problem: when I try to compile, it has an error that the defaults werent declared in the header file. I include them, and when I compile it says that those variables were already declared, and opens a completely irrelevant part of my project that has nothing to do with it.

If I take the declarations out of my .cpp file, it gives errors as well: compiler: "blabla was not declared in this scope..." me: "I know, because you were bitching about that."

So, is here some way for me to fix this. it wont let me include it, and it wont let me not include it. there aren't any other options, unless I'm writing it wrong.

Any help is appreciated! Thank you for your time, I'm very tired.

Also, I wouyld like to note I'm not includeing the header file in a .cpp file it has a function in, so thats not the problem.
Last edited on
By convention, the default parameter values are given in the function declaration rather than the definition. This means the default value are visible to all cpp files that include the header.

You can provide the default in the function definition instead, but then the defaults will only be usable within the cpp file, which isn't much use. But you cannot specify the default values in both!

By habit, I put the default value in comments in the function definition (see below).

default_vs and and default_s must be declared in the header and must be static.

I have to say that I have not seen default out parameters before. I take it that you know that you'll end up with a distinct default_vs and default_s for every cpp file that includes blablabla.h?

1
2
3
4
5
6
7
8
9
10
11
12
13
// blablabla.h

#include <vector>
#include <string>

using namespace std;

// now in header
static vector<string> default_vs;
static string default_s = string(); // must be static, too

// default param values given here
int blablabla(string bla = "", vector<string>& blabla = default_vs, string& alb = default_s);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// blablabla.cpp

#include "blablabla.h" // include header with function declaration (inc defaults)

// these have been moved to header
//static vector<string> default_vs;
//string default_s = string();

// default param values are not given here (except for in comments)
int blablabla(string bla /*= ""*/, vector<string>& blabla /*= default_vs*/, string& alb /*= default_s*/) {
	if(!bla.empty()) {
		alb = bla[0];
		for(size_t i = 0; i > bla.length(); ++i) {
			char ach[2] = {bla[i], '\0'};
			blabla.push_back(ach);
		}
		return blabla.size();
	}
	alb = "x";
	return 0;
}

1
2
3
4
5
6
7
8
9
// main.cpp

#include "blablabla.h"

int main() {
	int ret = 0;
	ret = blablabla();
	return 0;
}
Last edited on
Alright, I tried what you told me to and it worked... to an extent.

One of my header/.cpp files aren't cooperating. I put the declaration in the header and it keeps giveing the error:

C:\Users\Username\Desktop\C++\Selective Data Backup\dataunits.cpp|41|error: 'default_s' was not declared in this scope|


as though it wasn't declared in the header.

.cpp:

float convert_size(int size = 0, string& unit = default_s)

header:

1
2
3
4
5
using namespace std;

static string default_s = string();

float convert_size(int size = 0, string& unit = default_s);


It should work, but it doesn't.

I would also like to add that it was also declared in another header, but that shouldn't affect the way this operates, i believe.
Last edited on
Remove the default arguments from the definition of convert_size in the .cpp.
#1 your cpp file should read

float convert_size(int size /*= 0*/, string& unit /*= default_s*/) // don't redeclare defaults

(and have include the header declaring this function)

#2 I assume <string> is included before using namespace std; in you header?

#3 You said:

it was also declared in another header, ...

What is "it"?

You should really only declare something in one header and then include that header where it's needed.
Last edited on
no, by that I meant I included
static string default_s = string();

in another header.

Also, if I don't declare the defaults in the function, how can I use that function in the .cpp file it was defined in? I can't, because it's defaults aren't defined.

It was my understanding that defaults are defined where the function is written, not in the header files.

Correct me if I'm wrong.
Last edited on
You have to include the header declaring the function (along with the default) in the cpp file defining the function (without the defaults).

The compiler knows what to do with the function definition as its see the default values in the declaration.

The code I pasted above, which illustrates this, was compiled (successfully) before I posted it!

As far as defining default_s goes, you will hit an error if you include both headers as C++ doesn't allow multiple definitions.

Andy

PS for a bit more on the subject, see

Default value of function parameter
http://stackoverflow.com/questions/2842928/default-value-of-function-parameter

PPS trivia

static string default_s = string();

is the same as

static string default_s;

so you can save yourself some typing!
Last edited on
Topic archived. No new replies allowed.