collect2.exe: error: ld returned 1 exit status

尝试运行一段Hanoi代码. 用Terminal编译器运行没问题能出结果。但是用VS CODE运行就出这个:collect2.exe: error: ld returned 1 exit status。已经sava all啦。大神们帮忙看一看!!!

//Main.cpp

#include "Game.h"
#include <iostream>

int main(){
  Game g;

  std::cout << "Initial game state: " << std::endl;
  std::cout << g << std::endl;

  g.solve();

  std::cout << "Final game state: " << std::endl;
  std::cout << g << std::endl;

  return 0;
}
//Stack.cpp
#include "Stack.h"

#include <exception>
#include <iostream>
using std::cout;
using std::endl;

void Stack::push_back(const Cube & cube) {
  // Ensure that we do not push a cube on top of a smaller cube:
  if ( size() > 0 && cube.getLength() > peekTop().getLength() ) {
    std::cerr << "A smaller cube cannot be placed on top of a larger cube." << std::endl;
    std::cerr << "  Tried to add Cube(length=" << cube.getLength() << ")" << std::endl;
    std::cerr << "  Current stack: " << *this << std::endl;

    throw std::runtime_error("A smaller cube cannot be placed on top of a larger cube.");
  }

  cubes_.push_back(cube);
}

Cube Stack::removeTop() {
  Cube cube = peekTop();
  cubes_.pop_back();
  return cube;
}

Cube & Stack::peekTop() {
  return cubes_[cubes_.size() - 1];
}

unsigned Stack::size() const {
  return cubes_.size();
}

std::ostream& operator<<(std::ostream & os, const Stack & stack) {
  for (unsigned i = 0; i < stack.size(); i++) {
    os << stack.cubes_[i].getLength() << " ";
  }
  os << endl;
  return os;
}
//Stack.h
#pragma once

#include <vector>
#include "uiuc/Cube.h"
using uiuc::Cube;

class Stack {
  public:
    void push_back(const Cube & cube);
    Cube removeTop();
    Cube & peekTop();
    unsigned size() const;

    // An overloaded operator<<, allowing us to print the stack via `cout<<`:
    friend std::ostream& operator<<(std::ostream & os, const Stack & stack);

  private:
    std::vector<Cube> cubes_;
};
//game.cpp
#include "Game.h"
#include "Stack.h"
#include "uiuc/Cube.h"
#include "uiuc/HSLAPixel.h"

#include <iostream>
using std::cout;
using std::endl;

// Game default constructor
Game::Game() {
  // The initial default game state has three stacks four cubes.
  
  // Create the three empty stacks:
  for (int i = 0; i < 3; i++) {
    Stack stackOfCubes;
    stacks_.push_back( stackOfCubes );
  }

  // Create the four cubes, placing each on the [0]th stack:
  // - A blue cube of length=4, on the bottom
  // - A orange cube of length=3, on top of the blue cube
  // - A purple cube of length=2, on top of the orange cube
  // - A yellow cube of length=1 at the very top
  Cube blue(4, uiuc::HSLAPixel::BLUE);
  stacks_[0].push_back(blue);

  Cube orange(3, uiuc::HSLAPixel::ORANGE);
  stacks_[0].push_back(orange);

  Cube purple(2, uiuc::HSLAPixel::PURPLE);
  stacks_[0].push_back(purple);

  Cube yellow(1, uiuc::HSLAPixel::YELLOW);
  stacks_[0].push_back(yellow);
}

void Game::_move(unsigned index1, unsigned index2) {
  Cube cube = stacks_[index1].removeTop();
  stacks_[index2].push_back(cube);
}

void Game::_legalMove(unsigned index1, unsigned index2) {
  if (stacks_[index1].size() == 0 && stacks_[index2].size() > 0) {
    _move(index2, index1);
  } else if (stacks_[index1].size() > 0 && stacks_[index2].size() == 0) {
    _move(index1, index2);
  } else if (stacks_[index1].size() > 0 && stacks_[index2].size() > 0) {
    if (stacks_[index1].peekTop().getLength() < stacks_[index2].peekTop().getLength() ) {
      _move(index1, index2);
    } else {
      _move(index2, index1);
    }
  }
  
  cout << *this << endl;
}

void Game::solve() {
  while (stacks_[2].size() != 4) {
    _legalMove(0, 1);
    _legalMove(0, 2);
    _legalMove(1, 2);
  }
}

std::ostream& operator<<(std::ostream & os, const Game & game) {
  for (unsigned i = 0; i < game.stacks_.size(); i++) {
    os << "Stack[" << i << "]: " << game.stacks_[i];
  }
  return os;
}
//game.h

#pragma once

#include "Stack.h"
#include <vector>

class Game {
  public:
    Game();
    void solve();

    // An overloaded operator<<, allowing us to print the stack via `cout<<`:
    friend std::ostream& operator<<(std::ostream & os, const Game & game);

  private:
    std::vector<Stack> stacks_;

  private:
    void _move(unsigned index1, unsigned index2);
    void _legalMove(unsigned index1, unsigned index2);
};
//UIUC/Cube.cpp

#include "Cube.h"
#include "HSLAPixel.h"
#include <iostream>


namespace uiuc {  
  Cube::Cube(double length, uiuc::HSLAPixel color) {
    length_ = length;
    color_ = color;
  }

  double Cube::getLength() const {
    return length_;
  }

  double Cube::getVolume() const {
    return length_ * length_ * length_;
  }

  double Cube::getSurfaceArea() const {
    return 6 * length_ * length_;
  }

  void Cube::setLength(double length) {
    length_ = length;
  }
}
//UIUC/Cube.h

#pragma once

#include "HSLAPixel.h"


namespace uiuc {

  class Cube {
    public:
        //
        Cube(double length, HSLAPixel color);
        double getLength() const;
        void setLength(double length);

        double getVolume() const;
        double getSurfaceArea() const;

    private:
        double length_;
        HSLAPixel color_;
  };
}
//UIUC/HSLAPixel.cpp


#include "HSLAPixel.h"
#include <cmath>
#include <iostream>
using namespace std;

namespace uiuc {
  HSLAPixel HSLAPixel::BLUE = HSLAPixel(240, 1, 0.5);
  HSLAPixel HSLAPixel::ORANGE = HSLAPixel(30, 1, 0.5);
  HSLAPixel HSLAPixel::YELLOW = HSLAPixel(60, 1, 0.5);
  HSLAPixel HSLAPixel::PURPLE = HSLAPixel(270, 1, 0.5);

  HSLAPixel::HSLAPixel() {
    h = 0;
    s = 0;
    l = 1.0;
    a = 1.0;
  }

  HSLAPixel::HSLAPixel(double hue, double saturation, double luminance) {
    h = hue;
    s = saturation;
    l = luminance;
    a = 1.0;
  }

  HSLAPixel::HSLAPixel(double hue, double saturation, double luminance, double alpha) {
    h = hue;
    s = saturation;
    l = luminance;
    a = alpha;
  }
}
//UIUC/HSLAPixel.h

#pragma once

#include <iostream>
#include <sstream>

namespace uiuc {
  class HSLAPixel {
  public:
    //static uiuc::HSLAPixel ILLINI_ORANGE;
    //static uiuc::HSLAPixel ILLINI_BLUE;



    double h; /**< Hue of the pixel, in degrees [0, 360). */
    double s; /**< Saturation of the pixel, [0, 1]. */
    double l; /**< Luminance of the pixel, [0, 1]. */
    double a; /**< Alpha of the pixel, [0, 1]. */

    /**
     * Constructs a default HSLAPixel.
     * 
     * A default pixel is completely opaque (non-transparent) and white.
     * Opaque implies that the alpha component of the pixel is 1.0.
     * Lower alpha values are (semi-)transparent.
     */
    HSLAPixel();
    
    /**
     * Constructs an opaque HSLAPixel with the given hue, saturation,
     * and luminance values.
     * 
     * @param hue Hue value for the new pixel, in degrees [0, 360).
     * @param saturation Saturation value for the new pixel, [0, 1].
     * @param luminance Luminance value for the new pixel, [0, 1].
     */
    HSLAPixel(double hue, double saturation, double luminance);

    /**
     * Constructs an HSLAPixel with the given hue, saturation,
     * luminance, and alpha values.
     * 
     * @param hue Hue value for the new pixel, in degrees [0, 360).
     * @param saturation Saturation value for the new pixel, [0, 1].
     * @param luminance Luminance value for the new pixel, [0, 1].
     * @param alpha Alpha value for the new pixel, [0, 1].
     */
    HSLAPixel(double hue, double saturation, double luminance, double alpha);




    static HSLAPixel BLUE;
    static HSLAPixel ORANGE;
    static HSLAPixel YELLOW;
    static HSLAPixel PURPLE;

  };
}

 

把你之前运行的小黑窗程序关了,看看资源管理器确保已关闭之后再跑vscode

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632