有朋友功能的麻烦

时间:2016-03-29 20:31:45

标签: c++ 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* ='\0');
~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]='\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]='\0';
    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;
}

1 个答案:

答案 0 :(得分:2)

您有一个Board的数组,称为boardptr。但那么hh是什么?单板。它有什么作用?没有!但是你在incAttitude(hh);中使用它!它确实会增加高度,但不会在boardptr[i](您正在使用)上,而是在hh上。

其次,您永远不会设置att,这意味着海拔总是1.5(这是默认值)。因此,incAttitude会将号码添加到1.5。例如,您可以将att传递给incAttitude

<小时/> 然后,如果你的比较条件,为什么你再次设置董事会?它会重置用户选择的每个值。你不需要,因为你已经提前几行致电setBoard。您使用setBoard设置的数据会保存在该数组中,因此您无需使用完全相同的数据覆盖数据。只需使用boardptr[i].print()即可。 char数组,strcmp,原始指针和固定大小的数组来自C,你应该考虑其他选择:

  • 字符数组 - &gt; std::string
  • strcmp - &gt; if (str == "foo")
  • 原始指针 - &gt;智能指针(或者在您的情况下(因为它们用作数组)std::arraystd::vector - std::vector用于动态大小数组)
  • 固定大小的数组 - &gt; std::array
相关问题