Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;类变量返回乱码 我的一个C++作业有问题。我们使用一个类来打印电话号码列表,但我很难弄清楚如何让number变量打印我们应该与构造函数一起使用的默认值_C++_Function_Class_Variables - Fatal编程技术网

C++;类变量返回乱码 我的一个C++作业有问题。我们使用一个类来打印电话号码列表,但我很难弄清楚如何让number变量打印我们应该与构造函数一起使用的默认值

C++;类变量返回乱码 我的一个C++作业有问题。我们使用一个类来打印电话号码列表,但我很难弄清楚如何让number变量打印我们应该与构造函数一起使用的默认值,c++,function,class,variables,C++,Function,Class,Variables,如果你们中有人能给我指出正确的方向,举个例子或诸如此类的例子,我将不胜感激 #include <iostream> #include <string> class PhoneNumber { //This class will help the user make a list of phone numbers) private: int countryCode; //Stores the Country Code e.g

如果你们中有人能给我指出正确的方向,举个例子或诸如此类的例子,我将不胜感激

#include <iostream>
#include <string>

class PhoneNumber { //This class will help the user make a list of phone numbers)
        private:
                int countryCode; //Stores the Country Code e.g. +1
                int areaCode; //Stores Area Code e.g. (925)
                int number; //Stores Number e.g. 754-6978
                char type; //Stores the "type" of number. e.g. 'H' for home, 'B' for business, 'C' for cell.
                int year; //Stores the year the number was added, for some odd reason.
        public:
                PhoneNumber(); //Empty constructor
                PhoneNumber(int ccode, int acode, int num, char line, int year);
                PhoneNumber(int num, char line = 'B');
                PhoneNumber(int acode, int num, char line = 'C');
                void setCountry(int ccode);
                void setArea(int acode);
                void setNumber(int num);
                void setType(char line);
                void setYear(int yr);
                int getCountry() const;
                int getArea() const;
                int getNumber() const;
                char getType() const;
                int getYear() const;
                bool doubleDigits() const;
                void printNumber() const;
                void printPhoneNumberStats() const;
};

//The Default Constructor
PhoneNumber::PhoneNumber() {
        void setCountry(int ccode  = 43);
        void setArea(int acode = 800);
        void setNumber(int num = 8675309);
        void setType(char line = 'H');
        void setYear(int year = 1981);
}

//Fill out the Phone Number class
PhoneNumber::PhoneNumber(int ccode, int acode, int num, char line, int year) {
}

PhoneNumber::PhoneNumber(int num, char line) {
}

PhoneNumber::PhoneNumber (int acode, int num, char line) {
}

int PhoneNumber::getCountry() const {
        return countryCode;
}

int PhoneNumber::getArea() const {
        return areaCode;
}

int PhoneNumber::getNumber() const {
        return number;
}

char PhoneNumber::getType() const {
        return type;
}

int PhoneNumber::getYear() const {
        return year;
}

bool PhoneNumber::doubleDigits() const {
}  

void PhoneNumber::printNumber() const {
        std::cout << number << std::endl;
}

void PhoneNumber::printPhoneNumberStats() const {

}
如果您能解释为什么我在打印number变量时会收到垃圾,以及如何修复它,我将不胜感激。 当然,任何其他建议也将不胜感激

谢谢大家!!
Caitlin

编号
从未初始化。您的
printNumber()
方法应该从类的其他成员中生成
number
(如果您希望
-
在其中,则需要是
字符串),然后打印它

因此,您的
printNumber()
方法需要看起来更像这样

void PhoneNumber::printNumber() const {
    number = //whatever you need to do to initialize it
    std::cout << number << std::endl;
}
void PhoneNumber::printNumber()常量{
number=//初始化它所需的任何操作

std::cout您的
PhoneNumber
默认构造函数(
PhoneNumber::PhoneNumber()
)实际上并不初始化任何成员变量

PhoneNumber::PhoneNumber() {
    void setCountry(int ccode  = 43);
    void setArea(int acode = 800);
    void setNumber(int num = 8675309);
    void setType(char line = 'H');
    void setYear(int year = 1981);
}
这些行不初始化任何东西-它们声明函数,它们不调用对象中的现有函数

要使默认构造函数初始化成员值,可以执行以下操作:

PhoneNumber::PhoneNumber()
    : countryCode(43), areaCode(800), number(8675309), type('H'), year(1981)
{
}
或者,如果要使用“set”函数,请通过以下方式调用它们:

PhoneNumber::PhoneNumber() {
    setCountry(43);
    setArea(800);
    setNumber(8675309);
    setType('H');
    setYear(1981);
}

也许你的教授有一个可以回答问题的办公时间?有些构造器不给数据分配元素memebers@akonsu是的,但我没能得到我需要的帮助,因为其他学生占用了他的时间。谢谢!这真的很有帮助。
PhoneNumber::PhoneNumber() {
    setCountry(43);
    setArea(800);
    setNumber(8675309);
    setType('H');
    setYear(1981);
}