2013-09-26 20 views
-1

在下面的代碼,我收到錯誤:基操作數具有非指針類型克++編譯器錯誤

city.cc: In member function ‘std::vector<std::basic_string<char> > MyCity::get_neighbours()’: 
city.cc:25:42: error: base operand of ‘->’ has non-pointer type ‘std::pair<MyCity*, double>’ 
In file included from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:65:0, 
       from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/char_traits.h:41, 
       from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ios:41, 
       from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ostream:40, 
       from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/iostream:40, 
       from city.cc:1: 
/depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = std::basic_string<char>; _U2 = double; _T1 = MyCity*; _T2 = double]’: 
city.cc:18:50: required from here 
/depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_pair.h:111:39: error: cannot convert ‘const std::basic_string<char>’ to ‘MyCity*’ in initialization 

#include <iostream> 
#include <vector> 
#include <string> 

using namespace std; 

class MyCity { 
string name; 
std::vector<pair<MyCity*,double> > neighbours; 
public: 
MyCity() 
{ 
    // neighbours.clear(); 
} 
MyCity(string s, string s1, double d) 
{ 
    name = s; 
    neighbours.push_back(std::make_pair(s1,d)); 
} 
std::vector<string> get_neighbours() 
{ 
    std::vector<string> names; 
     for (size_t i = 0; i< neighbours.size(); ++i) 
     { 
     names.push_back(neighbours[i]->first->get_name()); 
    } 
    return names; 
} 
}; 

class MyState { 
vector<MyCity*> cities; 
string name; 
public: 
MyState() { } 
MyState(string s) { 
    name =s; 
} 
bool add_city(string name, string neigh, double d) 
{ 
    MyCity* c = new MyCity(name,neigh,d); 
    cities.push_back(c); 
    return true; 
} 
}; 
+1

使用鄰居[I]。首先 – AnatolyS

+0

@AnatolyS但除非我誤以爲這不是唯一的錯誤。在'neighbours.push_back(std :: make_pair(s1,d));'類型也是錯誤的。 – john

+0

閱讀您的錯誤消息:'不能在'初始化'中將'const std :: basic_string'轉換爲'MyCity *'。你認爲這應該如何工作?請不要使用指針!你顯然沒有理解這個概念,所以對自己好一點,不要再擔心了。僅使用對象作爲開始。 – stefan

回答

1

不要取消引用std::pair,因爲它不是指針。

std::vector<string> get_neighbours() 
{ 
    std::vector<string> names; 

    for (size_t i = 0; i< neighbours.size(); ++i) 
     names.push_back(neighbours[i].first->get_name()); 

    return names; 
} 

您還可以在你的構造一個問題,std::make_pair(s1, d)將返回std::pair<std::string, double>所以它不能推回給你的鄰居向量。

嘗試類似的東西:

MyCtiy(string s) 
    :name(s) 
{ 
} 

MyCity(string s, string s1, double d) 
    :name(s) 
{ 
    created_neighbours.emplace_back(new neighbour(s1)); 
    MyCity* city = created_neighbours.back().get(); 
    neighbours.push_back(std::make_pair(city, d)); 
    city->addNeighbour(this, d); 
} 

private: 
std::vector<std::unique_ptr<MyCity>> created_neighbours; 

void addNeighbour(MyCity* city, double d) 
{ 
    neighbours.push_back(std::make_pair(city, d)); 
} 

注意:您可以剝去addNeighbourd一部分,如果你不希望該協會是許多一對多。

編輯:修復addNeighbour給一個MyCity指針。創建一個created_neighbours集合來存儲(和免費)創建的鄰居。

0

3個錯誤可以在你的代碼中找到。請找出並改正下面兩行代碼中的

neighbours.push_back(std::make_pair(some_pointer_to_the_MyCity_object,d)); 

names.push_back(neighbours[i].first->get_name()); 

而且你要實現MyCity類GET_NAME()函數

現在應該編譯

相關問題