0

I have just learnt some object oriented programming concepts in Python, but I want to transfer this knowledge to C++, and I have trouble with basic implementation that used to be easy using Python.

#include<iostream>
using namespace std;

class Animal{

    char name;
    int age;

    public:
    Animal(char name, int age);
};

Animal::Animal(char name, int age) {
    this->name = name;
    this->age = age;
}

int main()
{
    Animal dog ("Megg", 10);

    cout << "Name: " dog.name <<endl;

    return 0;
}

When I compile this code, I get a lot of messages, such as:

error: no matching function for call  to 'Animal::Animal(const char[5], int)'
note: Animal::Animal(char, int) <near match>
note: candidate expects 1 argument, 2 provided

Thanks!

  • I got another error: invalid conversion from 'char*' to 'char' :/ – Vinícius Lopes Simões Jul 18 '15 at 01:07
  • 5
    Please just learn C++ systematically from a book? Point questions about extreme basics of the language (e.g. what types are) don't seem like a productive way to learn. – Kerrek SB Jul 18 '15 at 01:09
  • 7
    A `char` is not a string. Use `std::string` instead. – Baum mit Augen Jul 18 '15 at 01:09
  • 1
    Also, just guessing how things might work and getting the rest from random internet bits will either lead to you writing horrible C++ code or you writing no C++ code at all. Read a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn C++ systematically, it is not just some "Python with different syntax". – Baum mit Augen Jul 18 '15 at 01:12

1 Answers1

3

you don't need to do this->name = name in your constructor definition

"Megg" is a string literal. You can cast "Megg" into const char * but not into a char (this was most likely causing your error).

or better yet. You can use the C++ Standard Library string class std::string

#include <iostream>
#include <string>

class Animal{

    std::string name;
    int age;

    public:
    Animal(std::string name, int age);
    std::string getName() const;
    int getAge() const;
};

Animal::Animal(std::string Name, int Age) {
    name = Name;
    age = Age;
}

std::string Animal::getName() const {
  return name;
}

int Animal::getAge() const {
   return age;
}

int main()
{
    Animal dog ("Megg", 10);

    std::cout << "Name: " << dog.getName() << std::endl; // Error in this line. Missing << between "Name: " and dog.name

    return 0;
}

Some additional edits:

You should avoid using using namespace std as it takes everything in the Standard Library (from the files you've included) and puts it in the global namespace. You can instead use the scope resolution operator :: as seen above.

When you start working with multiple libraries you may encounter that both have a class named vector or string, or functions with the same name. The way to avoid this is to specify what namespace you want to use.

or alteratively you can do the following:

using std::cout;
using std::endl;
using std::string;

Additionaly in order for you program to work you need a way to access your object's member variables. You could do this by making the variables public or the better practice is to add accessor functions.

Timothy Murphy
  • 1,322
  • 8
  • 16
  • Thank you!! It worked, but I don't understand why the compiler keeps complaining: "expected ' ; ' before 'dog' "... – Vinícius Lopes Simões Jul 18 '15 at 01:18
  • @ViníciusLopesSimões: You missed one more thing: `cout << "Name: " dog.name < – Fabio says Reinstate Monica Jul 18 '15 at 01:19
  • There was another error. Check the my newest edit. I changed the cout to be cout << "Name: " << dog.name << endl; – Timothy Murphy Jul 18 '15 at 01:20
  • 1
    And by the way, this won't compile yet, because `name` is private, so you can't `cout` it from `main`. You either have to make it public, or you have to provide a public method that will either print it or return it. – Fabio says Reinstate Monica Jul 18 '15 at 01:25
  • `using namespace std;` is fine when not in a header file, just a matter of personal preference – M.M Jul 18 '15 at 03:13
  • `<< dog.name` should be `<< dog.Name()`. To OP: I'd suggest using a naming convention like `getName()` for "getter" functions. And in case you are unaware, it is not required to code like this; you could just make `name` a public variable and not have those getter functions. – M.M Jul 18 '15 at 03:13
  • Hi thanks for the comments. True. But I'm giving an example to someone who's new to programming in C++. It's good to give them examples that use these conventions to start with. There are many ways to accomplish this. – Timothy Murphy Jul 18 '15 at 03:38