executing a structs member functions

Status
Not open for further replies.
Let's say I have a struct that contains a member function:

code:<HR>
Code:
struct foo{
    bool deposit(float amount);
}

I define the function elseware in the code. How do I "execute" this function?

The following does not seem to work:

code:<HR>
Code:
<P>int main()
{
  double result;
  foo person;<P>  result = person.deposit(4.5);<P>  return 0;
}
 
Originally posted by shabadu:
What's the error you get?

The first thing I thought was "How the hell do you have a function in a struct? Isn't that what classes are for?".

That's what I thought but my C++ class hasn't even hit classes yet! In fact we have yet to touch on if/else statements, while statements, switches, pointers, etc... I'm not sure what my prof wants out of this assignment. Seems like a bad question 3 weeks after the start of a C++ course.

Proffy
 
code:
--------------------------------------------------------------------------------

struct foo{ bool deposit(float amount);}

--------------------------------------------------------------------------------

I define the function elseware in the code. How do I "execute" this function?

The following does not seem to work:and code:
--------------------------------------------------------------------------------

int main(){ double result; foo person; result = person.deposit(4.5); return 0;}

i need to see how ur are implementing your bool deposit function. bool is a boolean value meaning that if u call and run deposite it returns a boolean value (true or false) and not a number. from your program, it looks like you are trying to assign the value float value to a bollean value.. i dont think "4.5" is neither true nor false (1 or 0).
 

wb

Ars Legatus Legionis
25,208
Subscriptor
To reiterate what others have already said (and hopefully make it crystal clear), the only thing obivously wrong with your code above is that "result" should be declared as type "bool", rather than "double".

However, this doesn't mean that your implementation of deposit() doesn't also have errors that are causing problems. You didn't post that part of the code, though.
 

PowerJoe

Ars Scholae Palatinae
1,160
This works for me:
code:<HR>
Code:
struct foo
{  
    bool deposit(float amount);
};<P>
bool foo::deposit(float amount)
{
    // do stuff here...
    return true;
}<P>int main()
{  
    bool result;  
    foo person;  
    
    result = person.deposit(4.5);  
    return 0;
}
It also worked when I defined result as "double". I suppose the compiler doesn't mind converting from bool (0/1) to double, since you don't lose any info.

-PJ
 

earl

Well-known member
3,066
Did you put a foo:: before your implementation of deposit?

code:<HR>
Code:
//bleh.h
struct foo
{
  float money;
  bool deposit(float amount);
};<P>//bleh.cpp
include"bleh.h"
#include<assert.h>
#include<stdio.h><P>bool foo::deposit(float amount) 
{
  money+=amount;
}
bool deposit(float amount)
{
  assert(0);  //these two functions are different
}<P>int main(int argc, char** argv)
{
  foo a;
  a.deposit(0.7f);
  fprintf(stdout, "workeD!\n");
  return 0;
}<P>

The above works for me...

-earl-
 
Originally posted by earl:
Did you put a foo:: before your implementation of deposit?

<BLOCKQUOTE>code:<HR>
Code:
//bleh.h
struct foo
{
  float money;
  bool deposit(float amount);
};<P>//bleh.cpp
include"bleh.h"
#include<assert.h>
#include<stdio.h><P>bool foo::deposit(float amount) 
{
  money+=amount;
}
bool deposit(float amount)
{
  assert(0);  //these two functions are different
}<P>int main(int argc, char** argv)
{
  foo a;
  a.deposit(0.7f);
  fprintf(stdout, "workeD!\n");
  return 0;
}<P>

The above works for me...

-earl-<HR></BLOCKQUOTE>

How? There's no return statement in either function. Shouldn't your compiler return an error?

Anyway, I wanna write stupid examples too! Here's mine, ain't it purty?

code:<HR>
Code:
#include <iostream><P>using namespace std;<P>struct foo
{
  public:
    bool deposit (float amount);
};<P>bool foo::deposit (float amount)
{
  return true;
}<P>int main (int argc, char **argv)
{
  foo bar;
  bool yesno = bar.deposit (1.0f);<P>  if (yesno)
    cout << "Success\n";
  else
    cout << "Failure\n";<P>  return 0;
}

Compiles and runs on GCC 2.95.4, Linux.
 

Z Sphinx

Ars Tribunus Angusticlavius
7,626
Another thing that stands out in the original post:

code:<HR>
Code:
struct foo{    bool deposit(float amount);}

I know it's petty and perhaps a mistake in the posting, but there needs to be a semi-colon after the ending brace.

Also:

There's no return statement in either function. Shouldn't your compiler return an error?

There doesn't need to be. I suppose it depends on your compiler, but Visual C++ 6.0 doesn't care.
 
Status
Not open for further replies.