朋友功能出现问题
trouble with friend function
所以我的程序是,我希望用户输入关于一块板的信息,这样除了我需要用户输入态度值的部分之外,所有的东西都能工作,然后再加一个数字来增加态度,所以我只能这样做。正如你所看到的,我作为朋友声明了这个函数,但当我输入需要将其增加到态度值的数字时,它保持不变!我希望我把我面临的问题说清楚了!我很感激的帮助
#ifndef BOARD_H
#define BOARD_H
class Board {
friend void incAttitude(Board &);
public:
friend void incAttitude(Board &);
static int boardcount;
Board(int=5,int=3,double=1.5,char* =' ');
~Board();
Board &setBoard(int,int,double,char*);
char getLocation();
Board &print();
double surface();
private:
const int length;
int width;
double attitude;
char location[30];
};
#endif
#include<iostream>
#include<cstring>
#include<string>
#include<assert.h>
using namespace std;
#include "Board.h"
int Board::boardcount=0;
Board::Board(int l,int w,double a,char* lo)
: length(l)
{
width=w;
attitude=a;
location[0]=' ';
boardcount++;
}
Board::~Board(){
boardcount--;
}
Board &Board::setBoard(int l,int w,double a,char* lo)
{
width=(w>=2 && w<=5)?w:3;
attitude=(a>=1.5 && a<=2.8)?a:1.5;
strncpy_s(location,lo,29);
location[29]=' ';
return *this;
}
char Board::getLocation(){
return *location;
}
double Board::surface(){
return length*width;
}
void incAttitude(Board &h)
{
double incAttitude;
cout<<"enter to increase attitude "<<endl;
cin>>incAttitude;
h.attitude+=incAttitude;
cout<<"the attitude of the board after the increase : "<<h.attitude<<endl;
}
Board &Board::print(){
cout<<"the length of the boarad : "<<length<<endl;
cout<<"the width of the board : "<<width<<endl;
cout<<"the height of the board : "<<attitude<<endl;
cout<<"the location of the board : "<<location<<endl;
cout<<"the surface of the board : "<<surface()<<endl;
return *this;
}
int main(){
Board hh;
Board *boardptr;
int count,len,wid;
double att;
char locat[30];
cout<<"how many boards do you need to create ? "<<endl;
cin>>count;
boardptr = new Board[count];
assert(boardptr!=0);
for (int i=0; i<count; i++){
cout << "Enter the length: ";
cin >>len;
cout << "Enter the width: ";
cin >> wid;
cout << "Enter the attitude: ";
cin >> att;
incAttitude(hh);
cout<<"Enter the location: ";
cin >>locat;
boardptr[i].setBoard(len,wid,att,locat);
cout<<"------------------------------------------"<<endl;
if (strcmp("NewYork",locat) == 0)
{
boardptr[i].setBoard(len,wid,att,locat).print();
}
}
delete [] boardptr;
system("pause");
return 0;
}
您有一个Board
s的数组,称为boardptr
。但什么是hh
?一块木板。它做什么?没有什么但您正在incAttitude(hh);
中使用它!它确实增加了高度,但不是在boardptr[i]
(你正在使用的)板上,而是在hh
上。
其次,您从不设置att
,这意味着高度始终为1.5
(这是默认值)。所以incAttitude
将数字加到1.5
上。例如,您可以将att
传递给incAttitude
。
那么,如果你的比较条件,为什么要重新设置棋盘呢?它重置用户选择的每个值。您不需要,因为您已经提前几行呼叫了
setBoard
。使用setBoard
设置的数据保存在该数组中,因此不需要用完全相同的数据覆盖数据。只需使用boardptr[i].print()
即可。char数组、
strcmp
、原始指针和固定大小数组都来自C,您应该考虑其他选择:
- 字符数组->
std::string
strcmp
->if (str == "foo")
- 原始指针->智能指针(或者在您的情况下(因为它们被用作数组)
std::array
或std::vector
-std::vector
用于动态大小的数组) - 固定大小阵列->
std::array
相关文章:
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 解析模板的好友功能时出现问题
- creat_list2功能有什么问题?
- 此工厂功能有什么问题?
- 具有sleep_for和sleep_until功能的 gcc 可能存在的问题
- Sinlge 链表,C++,删除所有和搜索功能的问题
- VS2019中内联功能的一些问题
- 我的显示功能有问题
- MPI 二进制文件 I/O 基本功能和性能问题
- C++ 初级面试问题:仅使用字符指针压缩字符序列的功能
- C 功能性能问题
- 此读取功能有什么问题
- in_degree功能出现问题
- 我的选择排序功能C 有什么问题
- BOOST-ASIO ASYNC_RECEIVE_FROM功能超载问题( 动态指针)
- 朋友功能可访问性问题
- 我的C++作业中的"solve"功能有什么问题?
- 面临CPP中排序功能的问题
- 反向指针功能问题
- C++ lower_bound比较功能问题