Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2004

Tip: Looking for answers? Try searching our database.

Function must return a value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve McLellan - 18 May 2004 12:52 GMT
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
tom_usenet - 18 May 2004 16:03 GMT
>Hi,
>
[quoted text clipped - 19 lines]
>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
Signature

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Steve McLellan - 18 May 2004 16:14 GMT
> >Hi,
> >
[quoted text clipped - 26 lines]
> 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
Sven Groot - 18 May 2004 16:34 GMT
> 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;
}
----

This code properly gives the C4716 error message. This happens with or
without the /clr switch, and in fact still happens when I turn X into a __gc
class. Could you post a complete example that shows your symptoms?

Signature

Sven Groot

http://unforgiven.bloghorn.com

Steve McLellan - 18 May 2004 16:49 GMT
> > Hi,
> >
[quoted text clipped - 17 lines]
> }
> ----

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;
};
tom_usenet - 18 May 2004 17:41 GMT
>Try this.... UmTest should return something. (And don't tell me my code is
>horrible or I'll cry).
[quoted text clipped - 29 lines]
>   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
Signature

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Steve McLellan - 18 May 2004 18:00 GMT
> >Try this.... UmTest should return something. (And don't tell me my code is
> >horrible or I'll cry).
[quoted text clipped - 41 lines]
>
> Tom

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
Sven Groot - 18 May 2004 23:44 GMT
> 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.

Signature

Sven Groot

http://unforgiven.bloghorn.com

Ronald Laeremans [MSFT] - 18 May 2004 23:58 GMT
Thanks for the report. This is fixed in current builds of the Whidbey
release.

Ronald Laeremans
Visual C++ team

>> Try this.... UmTest should return something. (And don't tell me my
>> code is horrible or I'll cry).
[quoted text clipped - 34 lines]
> both in and the error is not generated. Again, this only applies when
> compiling with /clr.
Carl Daniel [VC++ MVP] - 19 May 2004 00:20 GMT
> 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
Sven Groot - 19 May 2004 00:46 GMT
>> This code correctly causes the error message without /clr, but
>> incorrectly compiles with /clr.
>
> 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!

Signature

Sven Groot

http://unforgiven.bloghorn.com

Carl Daniel [VC++ MVP] - 19 May 2004 01:31 GMT
>> 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!

<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
Steve McLellan - 19 May 2004 11:15 GMT
> >> This code correctly causes the error message without /clr, but
> >> incorrectly compiles with /clr.
[quoted text clipped - 6 lines]
> 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
Carl Daniel [VC++ MVP] - 19 May 2004 14:58 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.