Hướng dẫn passing object to constructor c++ - chuyển đối tượng tới hàm tạo C++

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

Hướng dẫn passing object to constructor c++ - chuyển đối tượng tới hàm tạo C++

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

Hướng dẫn passing object to constructor c++ - chuyển đối tượng tới hàm tạo C++

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

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 
0

Như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
  • Bàn luận
  • 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 
    
    1

    Vượ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 
    
    9

    Ví 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){};[...]
    
    5
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    6

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    0
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    1
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    2

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    3
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    4

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    expected identifier before '*' token 
    
    4

    expected identifier before '*' token 
    
    5
    expected identifier before '*' token 
    
    6

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    expected identifier before '*' token 
    
    8

    expected identifier before '*' token 
    
    9

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    8
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    9

    expected identifier before '*' token 
    
    4

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    class ShortestPath{
    public:
        ShortestPath(Graph& graph): graph(graph){};[...]
    private:
        Graph &graph; 
    }
    
    4

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    expected identifier before '*' token 
    
    1
    expected identifier before '*' token 
    
    2

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    class ShortestPath{
    public:
        ShortestPath(Graph& graph): graph(graph){};[...]
    private:
        Graph &graph; 
    }
    
    8

    ShortestPath(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; 
    }
    
    2

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    8
    class ShortestPath{
    public:
        ShortestPath(Graph& graph): graph(graph){};[...]
    private:
        Graph &graph; 
    }
    
    1

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    class 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; 
    }
    
    2

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    // note that (&graph) gets the address of the graph
    ShortestPath(Graph& graph): graph(&graph) {};
    
    6

    ShortestPath(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; 
    }
    
    2

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    8
    class ShortestPath{
    public:
        ShortestPath(Graph& graph): graph(graph){};[...]
    private:
        Graph &graph; 
    }
    
    1

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    class 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; 
    }
    
    2

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    ShortestPath(Graph& graph): graph(&graph) {};
                                      ^ // Get the pointer to object
    
    4
    ShortestPath(Graph& graph): graph(&graph) {};
                                      ^ // Get the pointer to object
    
    5

    expected identifier before '*' token 
    
    8

    Đầu ra

    cannot convert `Graph' to `Graph*' in initialization 
    
    2

    ShortestPath(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

    Syntax:  

    cannot convert `Graph' to `Graph*' in initialization 
    
    3

     class ShortestPath{
    public:
        ShortestPath(Graph& graph): graph(&graph){};[...]
    private:
        Graph *graph; 
    }
    
    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
    // note that (&graph) gets the address of the graph
    ShortestPath(Graph& graph): 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. 
    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 
    
    9

    Ví 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){};[...]
    
    5
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    6

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    0
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    1
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    2

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    class ShortestPath{
        public:
            ShortestPath(Graph& graph): graph(graph){};
    
        private:
            Graph &graph; 
                  ^ // A reference to object
    };
    
    9

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    expected identifier before '*' token 
    
    4

    expected identifier before '*' token 
    
    5
    cannot convert `Graph' to `Graph*' in initialization 
    
    03

    expected identifier before '*' token 
    
    5
    cannot convert `Graph' to `Graph*' in initialization 
    
    05

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    3
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    4

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    expected identifier before '*' token 
    
    8

    expected identifier before '*' token 
    
    9

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    8
    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    9

    expected identifier before '*' token 
    
    4

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    cannot convert `Graph' to `Graph*' in initialization 
    
    16

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    class ShortestPath{
    public:
        ShortestPath(Graph& graph): graph(graph){};[...]
    private:
        Graph &graph; 
    }
    
    6

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    class ShortestPath{
    public:
        ShortestPath(Graph& graph): graph(graph){};[...]
    private:
        Graph &graph; 
    }
    
    8

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    cannot convert `Graph' to `Graph*' in initialization 
    
    22

    ShortestPath(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; 
    }
    
    2

    ShortestPath(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; 
    }
    
    8
    cannot 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; 
    }
    
    8
    cannot 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; 
    }
    
    8
    cannot convert `Graph' to `Graph*' in initialization 
    
    41
     class ShortestPath{
    public:
        ShortestPath(Graph& graph): graph(&graph){};[...]
    private:
        Graph *graph; 
    }
    
    2

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    cannot convert `Graph' to `Graph*' in initialization 
    
    44

    ShortestPath(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; 
    }
    
    2

    ShortestPath(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; 
    }
    
    8
    cannot 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; 
    }
    
    8
    cannot 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; 
    }
    
    8
    cannot convert `Graph' to `Graph*' in initialization 
    
    41
     class ShortestPath{
    public:
        ShortestPath(Graph& graph): graph(&graph){};[...]
    private:
        Graph *graph; 
    }
    
    2

    ShortestPath(Graph& graph): *graph(graph){};[...]
    
    7
    ShortestPath(Graph& graph): graph(&graph) {};
                                      ^ // Get the pointer to object
    
    4
    ShortestPath(Graph& graph): graph(&graph) {};
                                      ^ // Get the pointer to object
    
    5

    expected identifier before '*' token 
    
    8

    Đầu ra

    cannot convert `Graph' to `Graph*' in initialization 
    
    4


    Bạn có thể chuyển đối tượng cho một hàm tạo?

    Bạn có thể sử dụng bất kỳ loại dữ liệu nào cho tham số của phương thức hoặc hàm tạo. Điều này bao gồm các loại dữ liệu nguyên thủy, chẳng hạn như đôi, phao và số nguyên, như bạn đã thấy trong phương thức tính toán và các loại dữ liệu tham chiếu, chẳng hạn như các đối tượng và mảng.. This includes primitive data types, such as doubles, floats, and integers, as you saw in the computePayment method, and reference data types, such as objects and arrays.

    Có thể truyền đối tượng như một đối số trong trình xây dựng sao chép không?

    Trả lời: Không, trong C ++, một hàm tạo sao chép không hỗ trợ vượt qua giá trị nhưng chỉ vượt qua tham chiếu.Nó không thể chấp nhận tham số đối tượng theo giá trị và nó phải luôn luôn nhận nó dưới dạng tham chiếu.Một lỗi, hàm tạo bản sao bất hợp pháp sẽ bị ném nếu chúng ta nhận được đối tượng theo giá trị.No, in C++, a copy constructor doesn't support pass by value but pass by reference only. It cannot accept object parameter by value and it should always receive it as a reference. An error, “Illegal copy constructor” will be thrown if we receive object by value.

    Tại sao chúng ta không thể chuyển một đối tượng theo giá trị cho một hàm tạo sao chép?

    Chuyển bằng giá trị (chứ không phải bằng tham chiếu) có nghĩa là một bản sao cần được thực hiện.Vì vậy, việc chuyển giá trị vào hàm tạo bản sao của bạn có nghĩa là bạn cần tạo một bản sao trước khi trình xây dựng bản sao được gọi, nhưng để tạo một bản sao, trước tiên bạn cần gọi Trình xây dựng sao chép.Lưu câu trả lời này.. So passing by value into your copy constructor means you need to make a copy before the copy constructor is invoked, but to make a copy you first need to call the copy constructor. Save this answer.

    Chúng ta có thể truyền các đối tượng dưới dạng tham số không?

    Vượt qua một đối tượng dưới dạng tham số rất giống với bất kỳ tham số nào khác.Bạn chỉ cần sử dụng tên của lớp đối tượng, trong trường hợp của bạn, thay cho bất kỳ loại biến nào khác.. You just need to use the name of the object class, in your case Fruit , in place of any other variable type.