1

I get

error: could not convert 'p1' from 'Person (*)()' to 'Person'

whenever i use the default constructor (when i create Person p1), i know it's because i'm using a char array but i have to use it, i can't use strings

i'm also getting 2 warnings

warning: converting to non-pointer type 'char' from NULL [-Wconversion-null]|

in the default constructor

warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]| 

when i create Person p2

so here's my code

#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

class Person{
   private:
    char* name;
    char gender;
    int age;
   public:
    Person();
    Person(char*, char, int);
    friend void printInfo(Person);
};

Person::Person()
:name(NULL), gender(NULL), age(0) // this results in the first warning
{
}
Person::Person(char* n, char g, int a)
:name(n), gender(g), age(a)
{
}
void printInfo(Person p){
 cout << "Name: " << p.name << endl;
 cout << "Age: " << p.age << endl;
 cout << "Gender: " << p.gender << endl;
}

int main()
{
Person p1(); // this results in an error
printInfo(p1);

Person p2("Max", 'm', 18); // this results in the second warning
printInfo(p2);

return 0;
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
3abkareeno
  • 59
  • 6

2 Answers2

10

You're running into the most vexing parse.

Person p1(); declares a function called p1 which returns a Person and takes no arguments. If you want to default construct a Person object called p1 simply say Person p1;

2

First warning comes because you try to assign NULL to gender which is a char.

Second one is because you assign const char pointer to a non-const char pointer in your object. Using char const * (or const char*) as your pointer type is one of the ways to get rid of this warning. Using char* const will not help, because it basically means const pointer to a modifyable string (i.e. you cannot change the pointer but you can change the string) which is not the case when you give it a string constant.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
  • ok i solved the first warning, but i don't know what to do in the second one, if i make the char* const i still get the same error – 3abkareeno Aug 13 '14 at 13:40
  • char* const and char const* are two different things, see here: http://stackoverflow.com/questions/4949254/const-char-const-versus-const-char – Ashalynd Aug 13 '14 at 13:45