【发布时间】:2014-01-23 11:50:32
【问题描述】:
我在从类中声明的方法返回结构时遇到问题。 struct i 由 3 个整数组成:day、month 和 year。 我使用类中的方法设置这三个值。
struct dmy{
int day, month, year;
};
dmy c;
然后我有一个方法可以返回完整的结构
dmy Date::getS(){
return c;
}
但我在编译时遇到很多错误。我该怎么办?
我也阅读了Is it safe to return a struct in C or C++?,但我的问题还没有解决。
程序由main.cpp、Difference.h和Date.h组成
Date.h 中的错误:
[Error] 'dmy' does not name a type
主要
using namespace std;
#include <iostream>
#include "Date.h"
int main(int argc, char** argv) {
Date date1;
Date date2;
cout<<"Insert first date\n";
date1.setAll();
return 0;
}
日期.h
class Date{
public:
Date(){
}
~Date(){
}
void setAll();
struct dmy{
int day, month, year;
};
dmy c;
dmy getS();
private:
void setDay();
void setMonth();
void setYear();
};
void Date::setAll(){
setDay();
setMonth();
setYear();
}
void Date::setDay(){
do{
cout<<"Type day: ";
cin>>c.day;
}while(0<c.day<31);
}
void Date::setMonth(){
//didn't right the checking again
cout<<"Type month: ";
cin>>c.month;
}
void Date::setYear(){
cout<<"Type year: ";
cin>>c.year;
}
dmy Date::getS(){
return c;
}
注意:该结构被称为'i'
【问题讨论】:
-
显示你的错误。对于有经验的人来说,错误可以准确地说明问题所在以及如何解决。
-
向我们展示您遇到的错误。
-
好吧,不幸的是,我正在用平板电脑写字,所以这并不容易。在学校,我们没有互联网接入。
-
你是否在头文件中声明
struct i?你包含那个头文件吗? -
另外,你可能不应该将你的结构命名为 i。 i 通常用作变量名。