Итак, я пытаюсь написать немного кода для работы с головоломками sudoku, и я продолжаю получать ошибки при попытке скомпилировать, и каждый раз, когда я его компилирую, он удаляет драйвер.Проблемы с компиляцией нескольких файлов
Heres код для водителя:
/*
* Jared C
* C++ Project
*/
#include <iostream>
#include "SudokuBoard.h"
using namespace std;
int main()
{
SudokuBoard board("sudoku.txt");
board.printBoard();
}
А вот файл заголовка
/*
* Jared C
* SudokuBoard.h
*/
#ifndef __SUDOKUBOARD_H_INCLUDED__
#define __SUDOKUBOARD_H_INCLUDED__
#include <iostream>
#include <string>
class SudokuBoard
{
private:
int content[9][9];
public:
SudokuBoard(std::string filename);
void printBoard();
int getNumAt(int, int);
void setNumAt(int, int, int);
};
#endif
И, наконец, судоку Board.cpp
/*
* Jared C
* SudokuBoard.cpp
*/
#include <iostream>
#include <fstream>
#include "stdlib.h"
#include "SudokuBoard.h"
using namespace std;
SudokuBoard::SudokuBoard(string filename)
{
string output;
string line;
int count = 0;
ifstream file;
file.open(filename.c_str());
if (file.is_open())
{
while (getline (file, line))
{
output += line;
}
file.close();
}
else cout<< "unable to open file" << endl;
for(int y = 0; y < 9; y++)
{
for(int x = 0; x < 9; x++)
{
content[x][y] = atoi(output.substr(count,1).c_str());
count ++;
}
}
}
void SudokuBoard::printBoard()
{
string output = "\n";
for(int y = 0; y < 9; y++)
{
if(y%3==0)
{
output += '\n';
}
for(int x = 0; x < 9; x++)
{
if(x%3==0)
{
output += " ";
}
output += content[x][y];
output += "\n";
}
}
cout<<output<<endl;
}
int SudokuBoard::getNumAt(int x, int y)
{
return content[x][y];
}
void SudokuBoard::setNumAt(int x, int y, int value)
{
content[x][y] = value;
}
Когда я называю gcc -c SudokuBoard.cpp
я получаю файл SudokuBoard.o просто отлично, но когда я тогда называю «gcc -o Driver.cpp SudokuBoard.o», я получаю огромная стена сообщений об ошибках, Heres образец из них:
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
SudokuBoard.cpp:(.text+0x34): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
SudokuBoard.cpp:(.text+0x43): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
И удаляет Driver.cpp Любая идея, что я делаю не так?
Re '__SUDOKUBOARD_H_INCLUDED__', идентификаторы с двумя последовательными символами подчеркивания зарезервированы для реализации, поэтому это формальный UB. –
Почему бы не выбрать ответ, который * решает вашу проблему * в качестве решения? не очень приятно читателям выбирать тот, который не работает. это похоже на активное неправильное направление. есть два ответа, которые работают. я предлагаю вам выбрать один из них. –
Решение, которое я выбрал как правильно, отлично сработало для меня, поэтому я выбрал его. – JCan