2017-12-10 7 views
0

전매 체 게임을 프로그래밍하고 있습니다. 그리고이 경우 플레이어가 그룹별로 소유 한 속성을 정렬하여 좋은 깨끗한 결과물을 만들기위한 정렬 기능을 정말 좋아합니다. 모든삭제 된 함수 연산자를 참조하려고하는 컴파일러 오류 C2280 =

먼저 여기에 플레이어는 이러한 속성의 목록을 소유하고, 이제 내 재산 분류기 기능

bool propertySort(Property a, Property b) { return a.getGroup() < b.getGroup(); } 

입니다.

#pragma once 
#include <string> 
#include <vector> 
#include "Dice.h" 
#include "Property.h" 

class Player 
{ 
public: 
    ... 
private: 
    int curMoney{ 1500 }; 
    int curLocation{ 0 }; 
    int playerNum{ 0 }; 
    std::string name; 
    std::vector<Property> ownedProperties{}; 
    int curDiceRoll; 
}; 

보시다시피 속성은 0 속성으로 초기화되었습니다.

메인에있는 내 속성 분류기에서 플레이어가 볼 수 있도록 보드에 속성을 표시 할 곳은 분류기 기능이 지속적으로 오류 C2280을 계속 제공합니다. 나는 정확하게이 정렬 함수가이 오류를 던지는 것을 알고있다. 왜냐하면 내가 프로그램을 잘 수행하는 정렬 줄을 주석 처리하기 때문이다. 내가 초보 프로그래머이기 때문에 내가 무지하다는 것은 분명하다. 누구라도 그것이 무엇인지에 대한 통찰력을 제공 할 수 있다면, 고마워요!

void viewProperties(Player& p) 
{ 
    string prompt = "Select a player to view their properties"; 
    vector<string> playerNames{}; 
    for (Player p : players) 
    { 
     playerNames.push_back(p.getName()); 
    } 
    Menu propertiesMenuSelection{ prompt, playerNames }; 
    vector<Property> propertiesOfSelectedPlayer = players[propertiesMenuSelection.getChoice()].getOwnedProperties(); 
    sort(propertiesOfSelectedPlayer.begin(), propertiesOfSelectedPlayer.end(), propertySort); 

    system("CLS"); 

는 경우이 여기에 도움이 속성 클래스를

#pragma once 
#include "Space.h" 
#include <array> 
#include <vector> 

extern std::vector<Player> players; 
class Property : public Space 
{ 
public: 
    Property(std::string name, std::vector<int> values); 
    ~Property(); 
    void run(Player&) override; 
    std::vector<std::string> boardOut() const override; 
    std::string getName() const override; 
    ... 
private: 
    std::string name; 
    int group; 
    int price; 
    int buildingCost; 
    int buildings; 
    Player* ownerPtr = nullptr; 
    std::array <int, 6> rent; 
    const std::array <std::string, 10> groups{ "Brown", "Light Blue", "Pink", "Orange", "Red", "Yellow", "Green", "Dark Blue", "Railroad", "Utility" }; 
}; 

있어 여기에 간단한 요청에 따라 공간 클래스입니다.

#pragma once 
#include <string> 
#include <vector> 

class Player; 

class Space 
{ 
public: 
    virtual void run(Player&) = 0; 
    virtual std::string getName() const = 0; 
    virtual std::vector<std::string> boardOut() const = 0; 
}; 

enter image description here

+0

어떤 부분 할당이 아니다. 귀하의 게시물에'스페이스'를 포함시킬 수 있습니까? –

+0

'give me error C2280' - 런타임 오류가 아니라 컴파일러 오류이므로 질문 제목에서 알 수 있듯이 오류가 발생하지 않습니다. – PaulMcKenzie

+2

'const std :: array' -이 멤버 변수가 복사를 사용할 수없는 이유 일 수 있습니다. – PaulMcKenzie

답변

2

삭제 된 기능 할당 연산자이다. const 구성원 groups을 할당 할 수 없기 때문에 Property을 할당 할 수 없습니다. 이에 대한 가장 논리적 인 해결책은 groupsstatic으로 선언하여 Property의 모든 인스턴스가 공유하므로 할당 할 필요가 없습니다. 예 :

static const std::array <std::string, 10> groups; 

스토리지 groups 필요가 할당되고 클래스 정의 외부 초기화 할 경우 :

const std::array <std::string, 10> Property::groups{ "Brown", "Light Blue", "Pink", 
                "Orange", "Red", "Yellow", "Green", 
                "Dark Blue", "Railroad", "Utility" }; 

TL; DR

는 할당 연산자의 기본 동작은 모든 복사하는 것 사본에 지능을 적용하지 않고 대상 회원에게 이 aconst하고 변화가없는 경우에도 변경할 수 없기 때문에 불가능합니다

test& operator=(const test & src) 
{ 
    a = src.a; 
    return *this; 
} 

의 동등한를 생성하는 정말 간단한 예를

class test 
{ 
public: 
    const int a = 10; 
}; 

을 의미한다.

당신은 당신의 자신의 할당 연산자

test& operator=(const test & src) 
{ 
    // deliberately does not assign a = src.a 
    return *this; 
} 

추가 할 수 있지만 초기화시 a의 값을 변경할 수있는있는 방법이없는 경우 당신이 원하는 것이 유일한 이유입니다.a을 변경할 수없는 경우 test의 모든 인스턴스는 a의 동일한 값을 가지며 astatic 멤버 일 수 있습니다. 두 testa들에 대해 다른 값을 가질 실패

예`또는`Property` Space`의

int main() 
{ 
    test a{11}; // a const variable cannot be changed after initialization, 
       // but this initializes a to 11. 
    test b;  // uses the default of 10 

    std::cout << a.a << ',' << b.a; // output 11,10 
    a = b; //this can't really be be done. a can never equal b 
} 
+0

완벽! 얼마나 훌륭한 설명인가. 그 정적 인 솔루션은 내가 필요로하고 구현 한 것입니다, 많은 감사합니다! – Alex

+1

@Alex 문제 없습니다. 너무 늦어서 당신을 도울 수는 없지만,이 문제를 가진 다음 사람이 그것을 찾길 바랍니다. – user4581301

+0

내 생각은 다른 사람들이이 오류를 겪는다면, 당신이 정렬과 같은 것을 사용하는 경우 매우 혼란 스러울 수 있습니다. 희망을 갖고, 그들은이 질문을 찾을 수 있습니다, 당신의 대답을보십시오. 그리고 아마도 그들은 비 정적 const의 위험하지 않은 사용이 얼마나 위험한 지 깨닫게 될 것입니다. 저와 다른 사람들에게도 위대한 교훈이었습니다! – Alex