Tôi muốn chuyển bằng cách tham chiếu một đối tượng cho một người đối tác, nhưng tôi gặp vấn đề, bởi vì tôi không biết làm thế nào để ràng buộc nó với biến của lớp.
Ở đây tôi đăng một số đoạn mã của tôi và lỗi tăng lên.
class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph *graph;
};
Trong trường hợp này, lỗi tăng là:
cannot convert `Graph' to `Graph*' in initialization
Và nếu tôi viết
ShortestPath[Graph& graph]: *graph[graph]{};[...]
Lỗi là
expected identifier before '*' token
Và khi tôi gọi hàm xây dựng, tôi có nên gọi như thế này không? ShortestPath [đồ thị];
Đã hỏi ngày 2 tháng 11 năm 2013 lúc 18:51Nov 2, 2013 at 18:51
0
Bạn phải thay đổi mã của mình theo cách này:
class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
or:
class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
Đã trả lời ngày 2 tháng 11 năm 2013 lúc 18:55Nov 2, 2013 at 18:55
Abdolahabdolahabdolah
5261 Huy hiệu vàng4 Huy hiệu bạc13 Huy hiệu đồng1 gold badge4 silver badges13 bronze badges
2
Hai giải pháp khả thi:
Vượt qua biểu đồ bằng cách tham chiếu và lưu trữ con trỏ
// note that [&graph] gets the address of the graph
ShortestPath[Graph& graph]: graph[&graph] {};
Vượt qua biểu đồ bằng con trỏ và lưu trữ con trỏ
ShortestPath[Graph* graph]: graph[graph] {};
Đã trả lời ngày 2 tháng 11 năm 2013 lúc 18:55Nov 2, 2013 at 18:55
Abdolahabdolahazz
5261 Huy hiệu vàng4 Huy hiệu bạc13 Huy hiệu đồng3 gold badges29 silver badges58 bronze badges
Hai giải pháp khả thi:
ShortestPath[Graph& graph]: graph[&graph] {};
^ // Get the pointer to object
Vượt qua biểu đồ bằng cách tham chiếu và lưu trữ con trỏ
class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};
private:
Graph &graph;
^ // A reference to object
};
Vượt qua biểu đồ bằng con trỏ và lưu trữ con trỏNov 2, 2013 at 19:27
Azzazzmasoud
5.7923 huy hiệu vàng29 Huy hiệu bạc58 Huy hiệu đồng16 gold badges134 silver badges204 bronze badges
2
Vì
cannot convert `Graph' to `Graph*' in initialization
5 của bạn là một con trỏ tới cannot convert `Graph' to `Graph*' in initialization
6, bạn nên sử dụng bên dưới [như một câu trả lời khác]:cannot convert `Graph' to `Graph*' in initialization
0Nhưng, nếu bạn chắc chắn rằng tuổi thọ của
cannot convert `Graph' to `Graph*' in initialization
5 đã vượt qua lớn hơn và bằng tuổi thọ của đối tượng ____ 18. Bạn có thể sử dụng tài liệu tham khảo thay vì con trỏ:Nov 2, 2013 at 18:52
Đã trả lời ngày 2 tháng 11 năm 2013 lúc 19:27Fred Larson
Masoudmasoud15 gold badges112 silver badges168 bronze badges
3
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Trong C ++, chúng ta có thể chuyển các đối tượng lớp của lớp dưới dạng đối số và cũng trả lại chúng từ một hàm giống như cách chúng ta vượt qua và trả về các biến khác. Không có từ khóa hoặc tệp tiêu đề đặc biệt nào được yêu cầu để làm như vậy. & NBSP;
Syntax:
cannot convert `Graph' to `Graph*' in initialization
1Vượt qua một đối tượng như đối số In this Example there is a class which has an integer variable ‘a’ and a function ‘add’ which takes an object as argument. The function is called by one object and takes another as an argument. Inside the function, the integer value of the argument object is added to that on which the ‘add’ function is called. In this method, we can pass objects as an argument and alter them.
Để truyền một đối tượng làm đối số, chúng tôi viết tên đối tượng làm đối số trong khi gọi hàm theo cách chúng tôi thực hiện cho các biến khác.syntax: & nbsp; & nbsp;
cannot convert `Graph' to `Graph*' in initialization
9Ví dụ: Trong ví dụ này, có một lớp có biến số nguyên ‘A và một hàm‘ Thêm, lấy một đối tượng làm đối số. Hàm được gọi bởi một đối tượng và lấy một đối tượng khác làm đối số. Bên trong hàm, giá trị số nguyên của đối tượng đối số được thêm vào đó là hàm ‘Thêm hàm được gọi. Trong phương pháp này, chúng ta có thể truyền các đối tượng như một đối số và thay đổi chúng. & Nbsp; & nbsp;
CPP
ShortestPath[Graph& graph]: *graph[graph]{};[...]
5ShortestPath[Graph& graph]: *graph[graph]{};[...]
6ShortestPath[Graph& graph]: *graph[graph]{};[...]
0 ShortestPath[Graph& graph]: *graph[graph]{};[...]
1 ShortestPath[Graph& graph]: *graph[graph]{};[...]
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
3 ShortestPath[Graph& graph]: *graph[graph]{};[...]
4ShortestPath[Graph& graph]: *graph[graph]{};[...]
7expected identifier before '*' token
4expected identifier before '*' token
5expected identifier before '*' token
6ShortestPath[Graph& graph]: *graph[graph]{};[...]
7expected identifier before '*' token
8expected identifier before '*' token
9ShortestPath[Graph& graph]: *graph[graph]{};[...]
7ShortestPath[Graph& graph]: *graph[graph]{};[...]
8 ShortestPath[Graph& graph]: *graph[graph]{};[...]
9expected identifier before '*' token
4ShortestPath[Graph& graph]: *graph[graph]{};[...]
7class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
4ShortestPath[Graph& graph]: *graph[graph]{};[...]
7expected identifier before '*' token
1 expected identifier before '*' token
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
7class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
8ShortestPath[Graph& graph]: *graph[graph]{};[...]
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
0 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
1 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
8 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
1ShortestPath[Graph& graph]: *graph[graph]{};[...]
7class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
6 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
8// note that [&graph] gets the address of the graph
ShortestPath[Graph& graph]: graph[&graph] {};
3 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
7// note that [&graph] gets the address of the graph
ShortestPath[Graph& graph]: graph[&graph] {};
6ShortestPath[Graph& graph]: *graph[graph]{};[...]
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
0// note that [&graph] gets the address of the graph
ShortestPath[Graph& graph]: graph[&graph] {};
9 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
8 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
1ShortestPath[Graph& graph]: *graph[graph]{};[...]
7class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
6 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
8// note that [&graph] gets the address of the graph
ShortestPath[Graph& graph]: graph[&graph] {};
3 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
7ShortestPath[Graph& graph]: graph[&graph] {};
^ // Get the pointer to object
4 ShortestPath[Graph& graph]: graph[&graph] {};
^ // Get the pointer to object
5expected identifier before '*' token
8Đầu ra
cannot convert `Graph' to `Graph*' in initialization
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
0 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
5 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
6
ShortestPath[Graph& graph]: *graph[graph]{};[...]
class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
Syntax:
cannot convert `Graph' to `Graph*' in initialization
3
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
8 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
9 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
0 In the above example we can see that the add function does not return any value since its return-type is void. In the following program the add function returns an object of type ‘Example'[i.e., class name] whose value is stored in E3. // note that [&graph] gets the address of the graph
ShortestPath[Graph& graph]: graph[&graph] {};
In this example, we can see both the things that are how we can pass the objects as well as return them. When the object E3 calls the add function it passes the other
two objects namely E1 & E2 as arguments. Inside the function, another object is declared which calculates the sum of all the three variables and returns it to E3.
This code and the above code is almost the same, the only difference is that this time the add function returns an object whose value is stored in another object of the same class ‘Example’ E3. Here the value of E1 is displayed by object1, the value of E2 by object2 and value of E3 by object3.
Để truyền một đối tượng làm đối số, chúng tôi viết tên đối tượng làm đối số trong khi gọi hàm theo cách chúng tôi thực hiện cho các biến khác.syntax: & nbsp; & nbsp;
cannot convert `Graph' to `Graph*' in initialization
9Ví dụ: Trong ví dụ này, có một lớp có biến số nguyên ‘A và một hàm‘ Thêm, lấy một đối tượng làm đối số. Hàm được gọi bởi một đối tượng và lấy một đối tượng khác làm đối số. Bên trong hàm, giá trị số nguyên của đối tượng đối số được thêm vào đó là hàm ‘Thêm hàm được gọi. Trong phương pháp này, chúng ta có thể truyền các đối tượng như một đối số và thay đổi chúng. & Nbsp; & nbsp;
CPP
ShortestPath[Graph& graph]: *graph[graph]{};[...]
5ShortestPath[Graph& graph]: *graph[graph]{};[...]
6ShortestPath[Graph& graph]: *graph[graph]{};[...]
0 ShortestPath[Graph& graph]: *graph[graph]{};[...]
1 ShortestPath[Graph& graph]: *graph[graph]{};[...]
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
7class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};
private:
Graph &graph;
^ // A reference to object
};
9ShortestPath[Graph& graph]: *graph[graph]{};[...]
7expected identifier before '*' token
4expected identifier before '*' token
5cannot convert `Graph' to `Graph*' in initialization
03expected identifier before '*' token
5cannot convert `Graph' to `Graph*' in initialization
05ShortestPath[Graph& graph]: *graph[graph]{};[...]
3 ShortestPath[Graph& graph]: *graph[graph]{};[...]
4ShortestPath[Graph& graph]: *graph[graph]{};[...]
7expected identifier before '*' token
8expected identifier before '*' token
9ShortestPath[Graph& graph]: *graph[graph]{};[...]
7ShortestPath[Graph& graph]: *graph[graph]{};[...]
8 ShortestPath[Graph& graph]: *graph[graph]{};[...]
9expected identifier before '*' token
4ShortestPath[Graph& graph]: *graph[graph]{};[...]
7cannot convert `Graph' to `Graph*' in initialization
16ShortestPath[Graph& graph]: *graph[graph]{};[...]
7class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
6ShortestPath[Graph& graph]: *graph[graph]{};[...]
7class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
8ShortestPath[Graph& graph]: *graph[graph]{};[...]
7cannot convert `Graph' to `Graph*' in initialization
22ShortestPath[Graph& graph]: *graph[graph]{};[...]
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
0 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
1 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
8 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
1 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
8cannot convert `Graph' to `Graph*' in initialization
33 // note that [&graph] gets the address of the graph
ShortestPath[Graph& graph]: graph[&graph] {};
0 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
8cannot convert `Graph' to `Graph*' in initialization
37 cannot convert `Graph' to `Graph*' in initialization
38 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
8cannot convert `Graph' to `Graph*' in initialization
41 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
7cannot convert `Graph' to `Graph*' in initialization
44ShortestPath[Graph& graph]: *graph[graph]{};[...]
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
0// note that [&graph] gets the address of the graph
ShortestPath[Graph& graph]: graph[&graph] {};
9 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
8 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[graph]{};[...]
private:
Graph &graph;
}
1 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
8cannot convert `Graph' to `Graph*' in initialization
33 // note that [&graph] gets the address of the graph
ShortestPath[Graph& graph]: graph[&graph] {};
0 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
8cannot convert `Graph' to `Graph*' in initialization
37 cannot convert `Graph' to `Graph*' in initialization
38 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
7 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
8cannot convert `Graph' to `Graph*' in initialization
41 class ShortestPath{
public:
ShortestPath[Graph& graph]: graph[&graph]{};[...]
private:
Graph *graph;
}
2ShortestPath[Graph& graph]: *graph[graph]{};[...]
7ShortestPath[Graph& graph]: graph[&graph] {};
^ // Get the pointer to object
4 ShortestPath[Graph& graph]: graph[&graph] {};
^ // Get the pointer to object
5expected identifier before '*' token
8Đầu ra
cannot convert `Graph' to `Graph*' in initialization
4