Cant use as function...?

This is a discussion on Cant use as function...? within the C++ Programming forums, part of the General Programming Boards category; damage needs to RETURN a value... im taking it returns int Code: int Damage::returnDamage ( int a, int b ) ...

  1. #16
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    damage needs to RETURN a value...

    im taking it returns int

    Code:
    int Damage::returnDamage ( int a, int b ) {
        int theDamage = 0;
    
       // do stuff
    
      return theDamage;
    }
    The return type you defined in the header file should be the return type you use in the inplementation. And also note the name of the function has to reflect what you called it in
    the header file too. The left side of the scope :: is the class name, the right side is the
    function name
    I'm just trying to be a better person - My Name Is Earl

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Nathan the noob View Post
    Damage::damage(int a, int b);
    arrg why wouldnt this work it says it expects something before int and int.... i dont under stand.

    And if their were

    void example(int, int)

    and in the main file i used

    ex.example(?, ?) how would i fill that in so it used a saved interger value like a = 3 cause it wont let me
    Probably caused by your CODE that is sat in the middle of your class declaration - the call to srand(). It probably belongs somewhere completely differently.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    And hey thanks still so many unanswered questions but u opened me up to classes witch seem to make stuff alot simpler once i figure it out

    ps: mat i deleted the srand


    And wat do u mean wat does return thedamage do/?
    Last edited by Nathan the noob; 02-17-2009 at 01:00 PM.

  4. #19
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    I used that as an example to show you how it "could" worK.

    If you header file had:

    Code:
    class Foo {
    private:
        int calculateDamage ( int, int );
    };
    That would work, as I returned the integer value I promised the class.
    I'm just trying to be a better person - My Name Is Earl

  5. #20
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    I made one that works but its simple
    Code:
    //Class//
    #ifndef PERSON_H
    #define PERSON_H
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Person {
          public:
                 string name;
                 int age;
                 string occupation;
                 int yearincome;
                 string position;
    };
    
    #endif
    Code:
    #include <iostream>
    #include "Person.h"
    using namespace std;
    
    int main(){
        Person a, b;
        a.name = "Bob";
        b.name = "Charlie the unicorn";
        a.age = 99;
        b.age = 1;
        a.occupation = "Farmer";
        b.occupation = "Cheese";
        a.yearincome = 1;
        b.yearincome = 1000000000000000000000000000000000000000;
        cout << a.name << "\n" << a.occupation << "\n" << a.age << "\n" << a.yearincome << "\n";
        cout << b.name << "\n" << b.occupation << "\n" << b.age << "\n" << b.yearincome << "\n"
        system("PAUSE");
    return 0;
    }
    Last edited by Nathan the noob; 02-17-2009 at 01:12 PM.

  6. #21
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Thats the basic idea yes.

    But you should never have all data members public as it goes against encapsualtion. Make them private and use functions, you could use getName() setName() for example to access them
    I'm just trying to be a better person - My Name Is Earl

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        b.yearincome = 1000000000000000000000000000000000000000;
    Unless you have some seriously large int-type in your particular compiler, that will overflow - a regular int will "only" handle a 10-digit number, eleven as long as the first digit is 2 and the following bits isn't over 147 million and some change.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Code:
    //Class//
    #ifndef PERSON_H
    #define PERSON_H
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Person {
          private:
                 string name;
                 int age;
                 string occupation;
                 int yearincome;
                 string position;
          public:
                 void getname(){
                      string nam = name;
                      }
                 
    };
    
    #endif
    Like that or do u have a better example?

    edit: Yer right mat it worked on my compiler the first time then i go to recompile it and it gave me a error :P

  9. #24
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Code:
    class Foo {
    public:
    std::string getName() {
       return m_Name;
    }
    
    void setName() {
        m_Name = "douglas";
    }
    
    private:
    std::string m_Name;
    };
    There is an error to two in that example but its more or less how its done
    I'm just trying to be a better person - My Name Is Earl

  10. #25
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Wat does return m_Name mean tho?

  11. #26
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Its return the data member.

    In main if I did this?

    Code:
     Foo fo;
    
    fo.setName();
    std::cout << fo.getName()  << std::endl;
    It would show the name
    I'm just trying to be a better person - My Name Is Earl

  12. #27
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Hmm well ive been experimenting a little and lookd around and cant find why this is being a problem
    Says incompatable types in the defined item section

    Code:
    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    #include "Person.h"
    #include "Item.h"
    using namespace std;
    
    int main(){
               Item cherry, ruby, wheat;
    ///////////////////////////////Defined items////////////////////////////////////
    cherry.amount = 56;         ruby.name = "Ruby";  wheat.name = "Wheat";
    cherry.price = 2;           ruby.price =60;        wheat.price = 4;
    cherry.name = "Cherry";   ruby.amount = 2;       wheat.amount = 104;
    ///////////////////////////////Input variables//////////////////////////////////
    char choice;
    ///////////////////////////////Special Variables////////////////////////////////
    int size;
    int x;
    ///////////////////////////////Dialog///////////////////////////////////////////
    char welcometoshop[]="Hello thier young sir would you like to look at my fine wares?\n";
    char trade[]="The trader pulls up a chest and as you peer in you see....";
    //////////////////////////Programstart//////////////////////////////////////////
    size=strlen(welcometoshop); //The shop keeper sayss hello 
    for(x=0;x<size;x++)
    {
       Sleep(30);
       printf("%c", welcometoshop[x]);
    }
      printf("<Y>es\n<N>o\n");
      for(;;)
      {
            choice=toupper(getch());
             if(choice=='Y')
             {
                system("cls");
                size=strlen(trade);
                for(x=0;x<size;x++)
                {
                    Sleep(30);
                    printf("%c", trade[x]);
                }
                Sleep(700);
                printf(cherry.name, "\tPrice:", cherry.price, "\tAmount:", cherry.amount, "\n");
                printf(ruby.name, "\tPrice:", ruby.price, "\tAmount:", ruby.amount, "\n");
                printf(wheat.name, "\tPrice:", wheat.price, "\tAmount:", wheat.amount, "\n");
             }
             else if(choice ='N')
             {
                break;  
             }
      }
             
    return 0;
    }
    Code:
    //Class//
    #ifndef ITEM_H
    #define ITEM_H
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Item {
          public:
                 char name[];
                 int price;
                 int amount;
    };
    
    #endif
    And with the little prog u made do u put the set name in the class cause then u really cant define it in the program
    And would this be correct
    Code:
    //Class//
    #ifndef ITEM_H
    #define ITEM_H
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Item {
          private:
                 char name[];
                 int price;
                 int amount;
          public:
            std::string getName() {
           return name;
            }
    
                 return price;
                 return amount;
        void setName() {
        name = "sword";
    }
    
    };
    
    #endif
    
    
    int main()
    {
    item.setName("sword2");
    std::cout << item.getName() << std::end1;
    Last edited by Nathan the noob; 02-17-2009 at 02:17 PM.

  13. #28
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    What error messages are you getting? I can see a few problems.
    I'm just trying to be a better person - My Name Is Earl

  14. #29
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    incompatable types in assignment of const char[5] [6] [7] to char[0u]

    i no it has soemthing to do with in the class char name[] and the fact that the names are longer then 1 char but ive done it before just not with classes

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Nathan the noob View Post
    incompatable types in assignment of const char[5] [6] [7] to char[0u]

    i no it has soemthing to do with in the class char name[] and the fact that the names are longer then 1 char but ive done it before just not with classes
    You have to declare an array with a size; you can't just say name[].

    Also, you have never assigned anything to an array, as that is not legal in C++, classes or no classes. My advice would be to destroy all char[] and go with std::string instead.

Page 2 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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