小编3ab*_*eno的帖子

将字符串转换为三个整数?

我有一个c ++项目,其日期类包含三个变量int day,month和year

class date{
  int day;
  int month;
  int year;
    public:
  date(); // default constructor
  date(int, int, int); // parameterized constructor with the three ints
  date(string) // this constructor takes a date string "18/4/2014" and assigns 
                  18 to intday, 4 to int month and 2014 to int year

};
Run Code Online (Sandbox Code Playgroud)

我想知道如何拆分字符串日期并将子字符串分配给int day,month和year三个变量

c++

1
推荐指数
1
解决办法
55
查看次数

错误:无法将'p1'从'Person(*)()'转换为'Person'

我明白了

error: could not convert 'p1' from 'Person (*)()' to 'Person'
Run Code Online (Sandbox Code Playgroud)

每当我使用默认构造函数时(当我创建Person p1时),我知道这是因为我使用的是char数组,但我必须使用它,我不能使用字符串

我也收到了2个警告

warning: converting to non-pointer type 'char' from NULL [-Wconversion-null]|
Run Code Online (Sandbox Code Playgroud)

在默认构造函数中

warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]| 
Run Code Online (Sandbox Code Playgroud)

当我创建Person p2时

所以这是我的代码

#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, …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
857
查看次数

标签 统计

c++ ×2