Struct定义的数据传递.我相信答案很简单
C++ Struct defined data passing. Simple answer im sure
我相信这是一个非常简单的修复,我觉得问它很愚蠢,但这里就是这样。
我需要一个结构体的帮助,并将信息从一个收集函数传递到一个保存或设置函数,然后再次传递给另一个函数以供进一步使用。
基本上,它看起来像这样开始。我只添加一小段代码。如果你想看的话,一切都可以提供。
我现在只是在寻找正确的方法来传递结构定义的数据从得到....设置…功能。
struct printype
{
char dots[8][15];
int unknown15; // can have values of 0..127
string serial11_14; // 8 characters 00000000...99999999
int year8; // without century, 0..99
int month7; // 1..12
int day6; // 1..31
int hour5; // 0..23
int minute2; // 0..59
};
int getunknown15(); // prototypes
int setunknown15(int);
那么我们有一个简单的main。
int main()
{
printype pt;
pt.unknown15=getunknown15();
pt.unknown15=setunknown15(12);
pt.serial11_14=getserial11_14();
pt.serial11_14=setserial11_14("12345678");
pt.year8=getyear8();
pt.year8=setyear8(44);
pt.month7=getmonth7();
pt.month7=setmonth7(11);
pt.day6=getday6();
pt.day6=setday6(12);
pt.hour5=gethour5();
pt.hour5=sethour5(12);
pt.minute2=getminute2();
pt.minute2=setminute2(23);
cout <<"-----------------------------------------------------"<<endl;
cout <<" Let's Get Started"<<endl;
cout <<"-----------------------------------------------------"<<endl;
setup(pt.dots); // sets up the array
dpinfo(pt); // prints out the final array
ftarray(pt);
spar(pt.dots);
darray(pt.dots);
}
,最后是get和set数组函数
int getunknown15()
{
printype tem;
cout <<"-----------------------------------------------------"<<endl;
cout <<" Enter the Unkown Variable (0-127): ";
cin >>tem.unknown15;
cout <<"-----------------------------------------------------"<<endl;
return tem.unknown15;
}
下一个是
int setunknown15(int tem)
{
printype pp;
if (tem>127||tem<0)
{
cout << "Error" << endl;
return 0;
}
else
{
pp.unknown15 = tem;
return pp.unknown15;
}
}
我希望这不是太多的阅读和理解
无论如何,我知道这个答案很简单,但是我的大脑现在就是不工作。
编辑:正如StilesCrisis所说,在这种情况下,Send结构体作为参数是非常愚蠢的。最好使用const引用。
哦,我不确定我是否正确理解了你的问题。您可以简单地将struct作为参数或指针发送给另一个函数。:
void SetStruct(const printype& var);
printype GetStruct();
这是你要找的吗?
请使用以下方式访问您的字段,(通过引用):
struct printype *myPtr = new printype;
myPtr->day6 = 43;
当使用指针而不是普通变量时,应该使用->代替。
我知道这有点老了,但我想我应该给它一个机会,因为你正在使用c++,看起来你正在尝试使用一些OO实践(我认为),你不需要从结构体开始,即使OO原则也可以使用它们,尽管不是那么优雅。
你可以这样定义你的类头文件
#ifndef PRINTYPE_H
#define PRINTYPE_H
#include <string>
using namespace std;
class printype
{
private: // we always want to declare our member fields private for safety/managements reasons, no one will be able to access them outside.
char dots[8][15];
int unknown15; // can have values of 0..127
string serial11_14; // 8 characters 00000000...99999999
int year8; // without century, 0..99
int month7; // 1..12
int day6; // 1..31
int hour5; // 0..23
int minute2; // 0..59
void init(); // This is the method we use to initialize our starting state.
public: // This is our public methods, how people deal with/get/set our state.
printype(); // This is our default constructor
printype(const printype& print_type); // This our copy constructor
virtual ~printype(); // This is our destructor, its virtual, making safer for inheritance.
// This is our setters/getters
void setUnknown(int unknown);
int getUnknown();
void setYear(int year);
int getYear();
void setMonth(int mont);
int getMonth();
// and well you get the idea, you can add more methods.
};
#endif
和随附的类源文件以及函数实现
printype::printype()
{
this->init(); // Initialize all your vatiables, safer to just define a function to this.
}
printype::printype(const printype& orig) // copy constructor
{
this->setUknown(orig.getUnknown());
this->setDay(orig.getDay());
this->setDots(orig.getDots());
// you get the idea ...
}
printype::~printype()
{
// Have anything you need to do before destroying the object.
}
void printype::init()
{
this->setUnknwon(0);
this->setyear(0);
this->setMonth(1);
char dots[8][15] = {' '};
this->setDots(dots);
// you get the idea, you want to initialize all your variables since, for the most part they initially hold garbage.
}
void printype::setUnknown(int unknown)
{
if (unknown >= 0 && unknown < 127)
this->unknown15 = unknown;
else
error("Expecting unknown to be between 0 and 127"); // error should probably print the error and/or exit(-1) up to u
}
int printype::setYear(int year)
{
if (year >= 1 && year <= 99)
this->year8 = year;
else
error("Expecting year between 0 and 99"); // you may want to implement an error function!
}
int printype::getYear()
{
return this->year8;
}
void printype::setDots(char dots[8][15])
{
// you may want to do some verifications
memcpy(this->dots, dots, sizeof(dots));
}
void printype::setDots(char **dots) // this is a bit unsafe, use at your own risk.
{
if (dots)
{
unsigned int index = 0;
for (index = 0; index < 8; index++)
if (dots[index])
memcpy(this->dots[index], dots[index], 15);
else
error("dots required pointer ...");
}
else
error("dots required pointer ...");
}
char **getDots() // We will be returning a copy, we don't want the internal state to be affected, from outside, by using reference or pointers.
{
char **dots = new char*[8];
unsigned int index = 0;
for (index = 0; index < 8; index++)
{
dots[index] = new char[15];
memcpy(dots[index], this->dots[index], 15);
}
return dots;
}
// and well you get the idea ...
使用你的类
printype *print_type_p = new print_type();
// or
printype pront_type_p();
// use the different public method to update the internal state.
print_type_p->setYear(3);
// or
print_type.setYear(3);
print_type_p->getYear();
// and so on.
相关文章:
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- 我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
- C ++分割错误,为什么使用"long long"我没有得到答案?
- 尝试检查输入的文本与我自己的自定义答案
- 我相信这是 clang++ 中的一个错误,与访问类的公共成员函数有关
- 我的代码是完整的,至少我相信它是...为什么它不能按预期运行?
- 我相信我为双链接列表编写的代码中的代码质量需要少量提高
- 无法在没有对象的情况下调用成员函数(尽管我相信我确实初始化了它)
- 我的c++程序中有3个(我相信是相关的)错误
- 我相信 -O2 标志导致我不得不在每台机器上重新编译.为什么
- 我如何用c++将我的答案作为固定的十进制美元金额
- 我相信这是clang中的一个错误,它与构造函数抛出的placement新表达式有关
- 我相信斯特劳斯图普的书,第三版第368页有一个错别字。有人可以确认吗?
- 我相信 §7.3.1.2/1 中的示例不正确
- Struct定义的数据传递.我相信答案很简单
- 如何让我的答案看起来像小数