Function Problems

Pages: 1234
Is there any way to make this function not throw an error during compile?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void opt (int number, char *variable) {
	char *name;
	char *type;
	switch (number) {
	case 2: {
		bool *value;
		name = "Option 3";
		value  = false;
		type = "bool";
	}
	break;
	case 1: {
		char *value;
		name = "Option 2";
		value = "Hello";
		type = "string";
	}
	break;
	default: {
		int value;
		name = "Option 1";
		value = 0;
		type = "number";
	}
	break;
	}
	if (variable == "name") return type;
	else if (variable == "type") return type;
	else return value;
}

I only started learning C/C++ recently & I'm doing this just for later reference on multi-type returns.
You can't do this.
If a function has a return type of void, it means that it does not return anything,
so these statement are errors
1
2
3
	if (variable == "name") return type;
	else if (variable == "type") return type;
	else return value;


*sigh*

Yes, and it's called a template.
http://cplusplus.com/doc/tutorial/templates/

-Albatross
Was that in reply to my answer??
Last edited on
No, actually... what you said is perfectly valid as far as we both know. Functions declared as void can't return anything in standard c++.

What I think he wanted to know was how to return stuff without being constrained by the type.

-Albatross
Thanks to both of you, I'll take a look at the link Albotross provided me (and yes your right about what I wanted to return)
Okay new problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
template <class option>
option opt (int number, char *variable) {
	char *name;
	char *type;
	switch (number) {
	case 2: {
		bool *value;
		name = "Option 3";
		value = false;
		type = "bool";
	}
	break;
	case 1: {
		char *value;
		name = "Option 2";
		value = "Hello";
		type = "string";
	}
	break;
	default: {
		int value;
		name = "Option 1";
		value = 0;
		type = "number";
	}
	break;
	}
	if (variable == "name") return type;
	else if (variable == "type") return type;
	else return value;
}

Compiler gives this (I omitted the problems from the main function):
Compiling: main.cpp
C:\Users\awsdert\Desktop\SOS\main.cpp: In function `option opt(int, char*)':
C:\Users\awsdert\Desktop\SOS\main.cpp:39: error: `value' was not declared in this scope
Process terminated with status 1 (0 minutes, 0 seconds)
12 errors, 0 warnings

I can't see where I'm going wrong.
What exactly is this function supposed to do?

-Albatross
Never mind, I fixed it.
Astonishing timing - as for what it supposed to do, it's to parse cmdline options into, I just needed to read the data first before implimenting writing to the data.
You can't return different types in the same function even using a template.

C++ is not like weakly typed languages such as Javascript.

Each function can only return one type. In a template function, that one type can vary. But within any particular invocation of the template function, only one type can be returned.
I'm not after all types at the same time, I'm after a type that's determind by the values I give the function.
Indeed. But that is simply not possible in C++.
Well... um... void*

-Albatross
You have to remember that when you call your function you need to know the return tybe before you call it:

1
2
3
4
5
6
7

int var = opt (2, "-size");

// need to declare the type of the variable
// that will accept the return value from the function
// BEFORE calling the function.
void* = evil
typedef void* evil;
Last edited on
I know, I know, don't get your deques in a knot.

But it would do (theoretically) what he/she wanted after an explicit type cast, no?

-Albatross
Also, you could declare multiple return types for this function. Like...

*deleted*

Actually, you can't do that without changing something about the variables. I really am short on coffee.

-Albatross
Last edited on
@Albatross

I recommend going to get a large coffee...
Pages: 1234