string help

Hi, Im doing a homework, which is about a graph of people, but Im getting 2 errors, hope somebody could help me:



This is the header:
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
#ifndef PERSON_H
#define PERSON_H
#include <string>

class person {
    public:
        /** Default constructor */
        person();

        /** Another  constructor */
       person(std::string ,std::string, int,bool, std::string[]);


               /** Default destructor */
        virtual ~person();


    private:
        std::string m_nickname; //!< Member variable "m_nickname"
        std::string m_fullname; //!< Member variable "m_fullname"
        unsigned int m_age; //!< Member variable "m_age"
        bool m_sex; //!< Member variable "m_sex"
        std::string m_hobbies[10]; //!< Member variable "m_hobbies"
    };

#endif // PERSON_H 



I think there is no problem, but here I do, the error says "error: expected ',' or '...' before 'hobbies'"
Also How do I initialize m_hobbies to be NULL?
This si the .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "person.h"
   person::person() {
    m_nickname="";
    m_fullname="";
    m_age=0;
    m_sex=false;
    m_hobbies ;// How do I initialize m_hobbies (which is an array os strings) to NULL?

    }
person::~person() {
    //dtor
    }
person::person(std::string nick, std::string name, int age, bool sex, std::string[] hobbies){
    m_nickname=nick;
    m_fullname=name;
    m_age=age;
    m_sex=sex;
    m_hobbies=hobbies;
    }




The error is in line 13
Last edited on
closed account (Dy7SLyTq)
r u a java programmer by any chance? ur style + the error makes me think so. we do arrays like this: string hobbies[] not string[] hobbies
Hahaha Yes , Im new around c++, but anyway, Thanks for helping me out! Btw, define my style please!
Last edited on
Hey, Im getting a new error, it says "error: incompatible types in assignment of 'std::string* {aka std::basic_string<char>*}' to 'std::string [10] {aka std::basic_string<char> [10]}'| " Would you help me?
closed account (Dy7SLyTq)
post ur code?
Well It's that one, Just change the std::string[] hobbies to std::string hobbies[]
closed account (Dy7SLyTq)
what are you passing to it?
In the past header file, I have this variable as private : (as you can see above)
std::string m_hobbies[10]; Then in the .cpp I have the constructor:
1
2
3
4
5
6
7
person::person(std::string nick, std::string name, int age, bool sex, std::string hobbies []){
    m_nickname=nick;
    m_fullname=name;
    m_age=age;
    m_sex=sex;
    m_hobbies=hobbies;
    }


So Im getting an error when I do this:m_hobbies=hobbies; The error says "incompatible types in assignment of 'std::string* {aka std::basic_string<char>*}' to 'std::string [10] "
Last edited on
Propose to init your array like this:

1
2
3
4
5
6
7
Person::Person(int numbHobbies, std::string hobbies [])
{
    for (int i = 0; i < numbHobbies && i < sizeof(m_hobbies); ++i)
    {
        m_hobbies[i] = hobbies[i];
    }
}
Topic archived. No new replies allowed.