0

I was working on some code for a class and checked everything, including a lot of similar errors, but nothing seem to be the same, could someone have a look?

I think it might have to do with c++ versions not being compatible with some code here but I'm not sure which.

I get the following error:

Undefined symbols for architecture x86_64:
  "__ZN15RobotController14initializeGameEi", referenced from:
      _main in ccZ9ege2.o
  "__ZN15RobotController8playGameEv", referenced from:
      _main in ccZ9ege2.o
  "__ZN15RobotControllerC1Ev", referenced from:
      _main in ccZ9ege2.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

main.cpp

#include <iostream>
#include "RobotController.h"
#include "Robot.h"

using namespace std;

int main()
{
  string inputBuffer;
  int numberOfPlayers;
  RobotController gameManager;

  /// get the number of players
  do
    {
      cout << "How many players:";
      getline(cin, inputBuffer);
      numberOfPlayers = atoi( inputBuffer.c_str() );
    }
  while (numberOfPlayers < 1 || numberOfPlayers > 9);

  /// play the game over and over
  do
    {
      gameManager.initializeGame(numberOfPlayers);
      gameManager.playGame();
      cout << endl << "Play Again? [y/n]";
      getline(cin, inputBuffer);
    }
  while ( tolower(inputBuffer[0]) != 'n' );

  return 0;
}

Robot.h

#ifndef ROBOT_H
#define ROBOT_H

#include <sstream>
using namespace std;

class Robot {

public:
  Robot();
  void setName(string name);
  string getName();
  void print();
  bool addWheel();
  bool addAxle();
  bool addTorso();
  bool addPlunger();
  bool addHead();
  bool addAntenna();
  bool addCell();
  int countWheel();
  int countAxle();
  int countHead();
  int countTorso();
  int countPlunger();
  int countAntenna();
  int countCell();
  bool complete();
  void initialize();

private:
  string playerName_;
  int wheels_, axles_, torsos_, plungers_, heads_, antennae_, cells_;
};

#endif

Robot.cpp

#include "Robot.h"
#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;

void Robot::initialize(){
    wheels_=axles_=torsos_=plungers_=heads_=antennae_=cells_=0;
}

void Robot::print() {
  cout << endl;
  cout << playerName_ << "\'s Robot Parts: ";
  cout << "Wheels: " << wheels_ << "| Axles: " << axles_
       << "| Torso: " << torsos_ << "| Plungers: " << plungers_
       << "| Heads: " << heads_ << "| Antenna: " << antennae_
       << "| Power Cells: " << cells_ << endl;
  cout << endl;
}

void Robot::setName(string name) { playerName_ = name; }

string Robot::getName() { return playerName_; }

bool Robot::addWheel() {
  if (wheels_ < 3) {
    wheels_++;
    return true;
  }
  return false;
}

bool Robot::addAxle() {
  if ((axles_ < 3) && (wheels_ > axles_)) {
    axles_++;
    return true;
  }
  return false;
}

bool Robot::addTorso() {
  if ((torsos_ == 0) && (axles_ == 3)) {
    torsos_++;
    return true;
  }
  return false;
}

bool Robot::addPlunger() {
  if ((plungers_ == 0) && torsos_ == 1) {
    plungers_++;
    return true;
  }
  return false;
}

bool Robot::addHead() {
  if ((heads_ == 0) && (torsos_ == 1)) {
    heads_++;
    return true;
  }
  return false;
}

bool Robot::addAntenna() {
    if ((antennae_ < 2) && (heads_ == 1)){
    antennae_++;
    return true;
    }
    return false;
}

bool Robot::addCell() {
  if ((cells_ < 4) && (torsos_ == 1)) {
    cells_++;
    return true;
  }
  return false;
}

int Robot::countWheel() { return wheels_; }
int Robot::countAxle() { return axles_; }
int Robot::countHead() { return heads_; }
int Robot::countTorso() { return torsos_; }
int Robot::countPlunger() { return plungers_; }
int Robot::countAntenna() { return antennae_; }
int Robot::countCell() { return cells_; }

bool Robot::complete() {
  if (wheels_ == 3 && torsos_ == 1 && axles_ == 3 && plungers_ == 1 &&
      heads_ == 1 && cells_ == 4 && antennae_ == 2) {
    return true;
  }
  return false;
}

RobotController.h

#ifndef ROBOTCONTROLLER_H
#define ROBOTCONTROLLER_H

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

class RobotController {
    public:
      RobotController();
      void initializeGame(int numPlayers);
      void playGame();
    private:
      vector<Robot> m_Robots_;
};

#endif

RobotController.cpp

#include <iostream>
#include <vector>
#include "RobotController.h"
#include "Robot.h"

void RobotController::initializeGame(int numPlayers)
{
    string name;
    m_Robots_.clear();
    m_Robots_.resize(numPlayers);

    for (int i = 0; i < numPlayers; i++){
        cout << "Player " << i+1 << " name: ";
        getline(cin, name);
        Robot robot;
        m_Robots_.push_back(robot);
        m_Robots_[i].setName(name);
        m_Robots_[i].initialize();
    }
}

void RobotController::playGame()
{
    int toss;
    srand(time(0));
    int k = 0;
    do{
        for (int i = 0; i < m_Robots_.size(); i++){
            cout << "Player " << i+1 << " starts" << endl;
            toss = rand() % 7 + 1;
            switch(toss){
                case 1:
                    m_Robots_[i].addWheel();
                    break;
                case 2:
                    m_Robots_[i].addAxle();
                    break;
                case 3:
                    m_Robots_[i].addTorso();
                    break;
                case 4:
                    m_Robots_[i].addPlunger();
                    break;
                case 5:
                    m_Robots_[i].addHead();
                    break;
                case 6:
                    m_Robots_[i].addAntenna();
                    break;
                case 7:
                    m_Robots_[i].addCell();
                    break;
            }
            m_Robots_[i].print();
        }
        k++;
    }while(!(m_Robots_[k].complete()));

    cout << "Winner is: " << m_Robots_[k].getName() << "!";

}
273K
  • 29,503
  • 10
  • 41
  • 64
Cobbi Liu
  • 1
  • 2
  • 1
    The syntax is correct in the header files, but you don't compile RobotController.cpp with main.cpp. – 273K Aug 03 '22 at 07:19
  • The code is not the problem. The problem is that you aren't compiling it. In other words the error is how you are building your program. You should say something about how you are doing that to get more specific help. – john Aug 03 '22 at 07:28
  • Nothing to do with C++ versions BTW – john Aug 03 '22 at 07:37

0 Answers0