【问题标题】:Displays nothing when debugged [duplicate]调试时不显示任何内容[重复]
【发布时间】:2012-12-27 09:39:15
【问题描述】:

可能重复:
How to pass objects to functions in C++?

主类

#include "List.h"
#include "Car.h"
#include "Worker.h"
#include "Queue.h"
#include <iostream>
#include <string>

using namespace std;
void initWorkerList(List<Worker>);
void initCarList(List<Car>, Queue, Queue);

int main() {
    List<Worker> WorkerList;
    List<Car> CarList;
    Queue q1, q2;
    initWorkerList(WorkerList);
    initCarList(CarList, q1, q2); // Error here
        //..... e.g cout << "Successful!"; but it does not displays it...
}

void initWorkerList(List<Worker> WorkerList) {
    Worker w1 = Worker("Ben Ang", "Ben123", "pass123", 'M');
    WorkerList.add(w1);
    Worker w2 = Worker("Grace Eng", "Gr4ce", "loveGrace", 'W');
    WorkerList.add(w2);
    Worker w3 = Worker("Rebecca Xuan", "Xuanz", "Rebecca Xuan", 'W');
    WorkerList.add(w3);
}

void initCarList(List<Car> CarList, Queue q1, Queue q2) {
    Car c1 = Car("SJS1006Z","Toyota", "Saloon car");
    Car c2 = Car("SFW6666E", "hyundai", "Taxi (Saloon)");
    Car c3 = Car("SCF1006G","Mercedes", "Large Van");
    Car c4 = Car("SBQ1006Z", "Puma", "Saloon Car");

    q1.enqueue(c1);
    q2.enqueue(c1);
    q2.enqueue(c3);
    q1.enqueue(c4);
    q1.enqueue(c1);
    q1.enqueue(c1);
    q1.enqueue(c1);
    q2.enqueue(c2);
    q2.enqueue(c2);
}

完全没有错误。但是在调试时没有显示任何内容......我已经尝试过,我的猜测是 initCarList(CarList,q1,q2); 有问题因为在该代码之后,其他代码完全可以工作。有什么问题吗?谢谢

【问题讨论】:

  • 嗯...考虑到您没有打印任何对我来说似乎是合理的行为...您还按值获取函数参数,因此您正在修改不再存在的副本当函数返回时。
  • 你用-g编译了吗?
  • @Sandeep 用 -g 编译它?那是网站还是应用程序?
  • @WhozCraig 好的!感谢您的指导!

标签: c++


【解决方案1】:

您通过值而不是通过引用传递队列变量。

initCarList(CarList, q1, q2); // Error here

因此 initCarList 中的任何更改都不会反映给调用者

将您的函数签名更改为

void initCarList(List<Car> CarList, Queue& q1, Queue& q2) {

和声明

void initCarList(List<Car>, Queue&, Queue&);

如果您按值传递参数,initCarList 内的任何更改都是函数范围内的本地更改,不会被反射回来。

Pass by Value
Caller              Callee

|------|            |------|
workedList          workedList
| ___  |            | ___  |
||   | |-------->   ||   | | <------
||___| |            ||___| |       |
|      |            |      |       |
|q1    |            |q1    |     (Changing any of these variables 
| ___  |            | ___  |      won't be reflected back)
||   | |-------->   ||   | |       |
||___| |            ||___| |       |
|      |            |      |       |
|q2    |            |q2    |       |
| ___  |            | ___  |       |
||   | |-------->   ||   | |       |
||___| |            ||___| | <------
|______|            |______|


Pass by reference

Caller              Callee

--------            --------
|wList |            |wList |
| ___  |            | ____ |
||   | |-------->   ||    || <------
||___|<|------------||-*__||       |
|   _  |            |      |       |
|q1    |            |q1    |     (Changing any of these variables 
| ___  |            | ____ |       will be reflected back)
||   | |-------->   ||    ||       |
||___|<|------------||-*__||       |
|      |            |      |       |
|q2    |            |q2    |       |
| ___  |            | ____ |       |
||   | |-------->   ||    ||       |
||___|<|------------||-*__|| <------
|______|            |______|

【讨论】:

  • 谢谢!多亏了你的画,我现在明白了! :D
  • 哇,这是一些不错的 ASCII 艺术:D
【解决方案2】:

您的函数按值传递,这意味着函数会复制传入的变量并在复制的变量上进行操作。要修改原始的,您需要通过引用传递参数

变化:

void initWorkerList(List<Worker>);
void initCarList(List<Car>, Queue, Queue);

收件人:

void initWorkerList(List<Worker> &);
void initCarList(List<Car>&, Queue&, Queue&);

【讨论】:

  • 谢谢!现在可以使用了! :D
【解决方案3】:

您通过值传递变量,这意味着函数的参数保存了它们的副本,您可以在函数结束时修改并丢弃它。通过引用传递来修改原始变量。例如,initCarList 将变为:

void initCarList(List<Car> CarList, Queue &q1, Queue &q2)

你也没有使用CarList参数,所以如果你的代码是这样的,你最好把它去掉。

【讨论】:

  • 好的。我已经删除了carlist alr。它也有效!谢谢! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 2011-04-12
  • 1970-01-01
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
相关资源
最近更新 更多