Function must return a value

S

Steve McLellan

Hi,

Is there any reason why the VC2003 C++ compiler emits the error "Function
must return a value" for global functions and not for class member
functions?

For example:

int test()
{

}
// Generates the error

class Test
{
int test() {} // Doesn't generate the error
};

I'm compiling with /clr if that makes any difference (though the class in
question is not managed). Warning level's 3, but even on four it doesn't
appear and as I've said, the error gets thrown up for global funcs. For once
I WANT my code not to compile :)

Steve
 
T

tom_usenet

Hi,

Is there any reason why the VC2003 C++ compiler emits the error "Function
must return a value" for global functions and not for class member
functions?

For example:

int test()
{

}
// Generates the error

class Test
{
int test() {} // Doesn't generate the error
};

I'm compiling with /clr if that makes any difference (though the class in
question is not managed). Warning level's 3, but even on four it doesn't
appear and as I've said, the error gets thrown up for global funcs. For once
I WANT my code not to compile :)

This doesn't generate an error either:
inline int test(){}

However, if you actually try to call Test::test or the test above, the
error is then generated. Note that this is not a diagnostic required
for a C++ compiler to be conforming.

Tom
 
S

Steve McLellan

tom_usenet said:
This doesn't generate an error either:
inline int test(){}

However, if you actually try to call Test::test or the test above, the
error is then generated. Note that this is not a diagnostic required
for a C++ compiler to be conforming.

Hi,

This is the problem; I AM calling it. My function is returning a vector, and
is being called from a function in a managed class. If there's no return, I
get a null reference exception at runtime, and no compiler troubles. I
understand that I won't necessarily get errors for functions that will be
compiled out. I'll try to knock up a simple test case that's more like my
code later if I have time.

Cheers,

Steve
 
S

Sven Groot

Steve said:
Hi,

Is there any reason why the VC2003 C++ compiler emits the error
"Function must return a value" for global functions and not for class
member functions?

I can't get that to reproduce here:
----
class X
{
public:
int test() { }
};

int main()
{
X x;
x.test();
return 0;
}
 
S

Steve McLellan

Steve said:
I can't get that to reproduce here:
----
class X
{
public:
int test() { }
};

int main()
{
X x;
x.test();
return 0;
}

Try this.... UmTest should return something. (And don't tell me my code is
horrible or I'll cry).

#include <map>
class UmTestClass
{
public:
map<int, int> UmTest()
{
map<float, float> myMap;
map<float, float>::const_iterator i( myMap.begin() );
while ( i != myMap.end() )
{
// Do some stuff
i++;
}
}
};

__gc class MTestClass
{
public:
MTestClass()
{
umTestClass = new UmTestClass();
}
~MTestClass()
{
delete umTestClass;
}
void MTest() { umTestClass->UmTest(); }
UmTestClass *umTestClass;
};
 
T

tom_usenet

Try this.... UmTest should return something. (And don't tell me my code is
horrible or I'll cry).

#include <map>
class UmTestClass
{
public:
map<int, int> UmTest()
{
map<float, float> myMap;
map<float, float>::const_iterator i( myMap.begin() );
while ( i != myMap.end() )
{
// Do some stuff
i++;
}
}
};

__gc class MTestClass
{
public:
MTestClass()
{
umTestClass = new UmTestClass();
}
~MTestClass()
{
delete umTestClass;
}
void MTest() { umTestClass->UmTest(); }
UmTestClass *umTestClass;
};

int main()
{
MTestClass m;
m.MTest();
}

Fixing your code so that it doesn't use __gc, has no errors and has a
main method caused a clean compile when /clr is used, and a warning
when not used. I don't know why /clr is affecting this.

Tom
 
S

Steve McLellan

Try this.... UmTest should return something. (And don't tell me my code is
horrible or I'll cry).

#include <map>
class UmTestClass
{
public:
map<int, int> UmTest()
{
map<float, float> myMap;
map<float, float>::const_iterator i( myMap.begin() );
while ( i != myMap.end() )
{
// Do some stuff
i++;
}
}
};

__gc class MTestClass
{
public:
MTestClass()
{
umTestClass = new UmTestClass();
}
~MTestClass()
{
delete umTestClass;
}
void MTest() { umTestClass->UmTest(); }
UmTestClass *umTestClass;
};

int main()
{
MTestClass m;
m.MTest();
}

Fixing your code so that it doesn't use __gc, has no errors and has a
main method caused a clean compile when /clr is used, and a warning
when not used. I don't know why /clr is affecting this.

Tom[/QUOTE]

Oh well, at least it's not me. Bit of a pain in the rear. Maybe Microsoft
are punishing lazy developers who can't be bothered to finish writing
implementation code. Is there anywhere to post these kinds of 'features'?
['Fixed in Whidbey', comes the reply]

Steve
 
S

Sven Groot

Steve said:
Try this.... UmTest should return something. (And don't tell me my
code is horrible or I'll cry).

I've done some tinkering, and this is the minimal program I can write that
exhibits the behaviour:
----
class X
{
public:
~X() { }
};

class Y
{
public:
int test()
{
X x;
}
};

int main()
{
Y y;
y.test();
return 0;
}
----

This code correctly causes the error message without /clr, but incorrectly
compiles with /clr.

The conditions for the bug occurring apparently is the presence of local
variable of a class type with a explicitly defined destructor. Remove the
destructor from X and the error is correctly generated. Remove the
definition of X x from Y::test and the error is correctly generated. Leave
both in and the error is not generated. Again, this only applies when
compiling with /clr.
 
R

Ronald Laeremans [MSFT]

Thanks for the report. This is fixed in current builds of the Whidbey
release.

Ronald Laeremans
Visual C++ team
 
C

Carl Daniel [VC++ MVP]

Sven said:
This code correctly causes the error message without /clr, but
incorrectly compiles with /clr.

Thanks for doing all the repro reduction work :)

This appears to be fixed in Whidbey.

-cd
 
S

Sven Groot

Carl said:
Thanks for doing all the repro reduction work :)

No problem. I might only be 22, but my first summer vacation job was testing
and debugging code for Unit4Agresso (then just Unit4). And as a long time MS
beta tester I know what is needed for a good repro scenario. Besides, I like
doing this kind of thing, it's good exercise and I always want to know
exactly what's going wrong just for my own peace of mind. ^_^
This appears to be fixed in Whidbey.

That's good to know. You know, I'm getting the impression from you that
Whidbey will be some kind of developer heaven. Is it even still possible to
find a bug in 2003 that's not already fixed in Whidbey? ^_~ And that so
early in the development stage... I can hardly wait!
 
C

Carl Daniel [VC++ MVP]

Sven said:
That's good to know. You know, I'm getting the impression from you
that Whidbey will be some kind of developer heaven. Is it even still
possible to find a bug in 2003 that's not already fixed in Whidbey?
^_~ And that so early in the development stage... I can hardly wait!

<G> Well, yes - I've got a number of bugs logged against Whidbey that are
also present in 2003. As you say, there's still quite a few months before
Whidbey ships - maybe some of those bugs will yet get fixed before Whidbey
ships.

In any case, Whidbey will rock, I'm sure of that - especially if you want to
do managed development in C++.

-cd
 
S

Steve McLellan

Carl said:
No problem. I might only be 22, but my first summer vacation job was testing
and debugging code for Unit4Agresso (then just Unit4). And as a long time MS
beta tester I know what is needed for a good repro scenario. Besides, I like
doing this kind of thing, it's good exercise and I always want to know
exactly what's going wrong just for my own peace of mind. ^_^

Steal my thunder, would you!? :) Maybe I just need to learn to finish
writing functions... I think I get bored and move onto something else.
Right, hangover coffee required. If anyone ever gets/works out an
explanation for this, let me know.

Steve
 
C

Carl Daniel [VC++ MVP]

Steve said:
Steal my thunder, would you!? :) Maybe I just need to learn to
finish writing functions... I think I get bored and move onto
something else. Right, hangover coffee required. If anyone ever
gets/works out an explanation for this, let me know.

It's just a bug in the compiler - what more explanation are you looking for?

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top