Help with function without object.

Hello!
Im writing a program to practice creating objects/classes.
I only ran into one problem which is when I try to create my print function. I want to create a function in my separate file that prints the objects attributes that I can call but it wont let me create it without an object. How do I fix this?
Thanks!
This is the function;
1
2
3
4
5
6
  void SkrivUt(){
std::cout << "Race: "<< Cat::getRace() << "Character:  " << Cat::getCharacter() << "Age: " << Cat::getAge();
}

I get the error message cannot call getRace.. getCharacter.. etc without member object.
hiya

Im writing a program to practice creating objects/classes.

You haven't created an object. You want to do something like this:

Cat myCat;

then you have your object to call you class methods from. Your class is just a blueprint really.
I'm guessing all of those functions aren't static.

You made a class called Cat, which is a type. A possible simile is that a class is like a blueprint for an object. It describes your object. Now, to actually use getRace and such, you need to actually have an object to call them from. The distinction here is similar to the difference between int (the type), and a variable of type int.

An example might look something like this.
1
2
3
Cat kitty;
//Do stuff to the kitty.
std::cout << "The kitty is " << kitty.getAge() << " year(s) old!\n";


Note that you can pass objects to functions and return them from functions like any other variable. Small complication: it may be better to have const references to objects as parameters if you don't need or want to copy them.

For more information: http://www.cplusplus.com/doc/tutorial/classes/

Happy programming!
-Albatross

EDIT: Ninja'd by mutexe.
Last edited on
Only static class functions can be called without an object using the Class::function() syntax.
So, you should add static as a keyword to the definition of your functions inside the Cat class.

However, you probably *don't* want to do this. Is every cat the same race, character, age? I'm assuming not, since that would be kinda useless. You probably do want to pass an instance of a Cat into your print function.

Or, keep everything inside the class:
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
32
class Cat {
  public:
    Cat(std::string race_, std::string character_, int age_);
    std::string getRace();
    std::string getCharacter();
    int getAge();
    void SkrivUt();

  private:
    std::string race;
    std::string character;
    int age;
  
};
Cat::Cat(std::string race_, std::string character_, int age_)
  : race(race_), character(character_), age(age_)
  {}
std::string Cat::getRace() { return race; }
std::string Cat::getCharacter() { return character; }
int Cat::getAge() { return age; }

void Cat::SkrivUt()
{
  std::cout << "Race: "<< getRace() << "Character:  " << getCharacter() << "Age: " << getAge();
}

int main()
{
    Cat cat(/*constructor args*/);
    cat.SkrivUt();
}

Last edited on
Hey! Thanks for your answers. Indeed they do not have the same attributes! But I would like to create a function that I can call whenever Ive created an object and want to print its attributes, what would be the best way of going about this?
Last edited on
Both Ganado and TheRabbitologist have given good examples mate.
Haha right, Im seeing double atm, been coding for 12h. Thanks lads will try to get it working!
Here's a compilable example of what you might be looking for
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
32
33
34
35
36
37
38
#include <iostream>
#include <string>

class Cat {
  public:
    Cat(std::string race_, std::string character_, int age_);
    std::string getRace();
    std::string getCharacter();
    int getAge();
    void SkrivUt();

  private:
    std::string race;
    std::string character;
    int age;
  
};
Cat::Cat(std::string race_, std::string character_, int age_)
  : race(race_), character(character_), age(age_)
  {}
std::string Cat::getRace() { return race; }
std::string Cat::getCharacter() { return character; }
int Cat::getAge() { return age; }

void Cat::SkrivUt()
{
  std::cout << "Race: "<< getRace() << " Character:  " << getCharacter() << " Age: " << getAge() << std::endl;;
}

int main()
{
    Cat cat1("Tabby", "Cute", 4);
    Cat cat2("Persian", "Lazy", 11);
    Cat cat3("Sphynx", "Quiet", 9);
    cat1.SkrivUt();
    cat2.SkrivUt();
    cat3.SkrivUt();
}


Each object has a print statement.
Ohh, turns out all that was missing was the Cat:: in front of function haha, duh... palms were faced. Much thanks for all your help tho, hopefully I can go to bed soon hah. ^^
Topic archived. No new replies allowed.