0

自从我用 C++ 编程以来已经有几年了。我的类代码编译但我的主要出现了错误:

/var/tmp//cc1Canw1.o: In function `main':
    main.cc:(.text+0x184): undefined reference to
   `Wordoku::valid( std::vector< std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
                                 std::allocator<std::basic_string<char, std::char_traits<char>, > std::allocator<char> > > > const&,
                    std::basic_string<char, std::char_traits<char>, >std::allocator<char> > const& )'
    collect2: ld returned 1 exit status

这是我的代码:

主文件

#include <iostream>
#include <string>
#include <vector>
#include "Wordoku.h"
#include <cassert>
using namespace std;

int main() {
  Wordoku obj;
  int i=1;
  while (!cin.eof()) {
    string letters;
    cin >> letters;
    //cout << letters << endl;

  if (!cin.fail()) {
      assert(letters.length()==9);
      vector<string> board;
      for (int j=0;j<9;j++) {
    string t;
    cin >> t;
    board.push_back(t);
      }

      bool res;
      cin >> res;
      //cout << res << endl;

      if (obj.valid(board, letters)==res) {
    cout << "Test " << i << " passed" << endl;
      }
      else {
        cout << "Test " << i << " failed" << endl;
      }
      i++;
    }
  }
}

Wordoku.h

//include the string, vector, and algorithm (used for sort) std libraries
#ifndef _WORDOKU_H
#define _WORDOKU_H
#include <vector>
#include <string>

//define the class Wordoku
class Wordoku{
    //no private variables or functions, 1 public function
    public:
        //valid tests if wordoku_board matches the definition of a completed wordoku board
        bool valid(const vector<string> &wordoku_board, const string &letters);
};

#endif // WORDOKU_H

词库.cc

//include the string, vector, and algorithm (used for sort) std libraries
#include <vector>
#include <string>
#include <algorithm>
#include "Wordoku.h"

using namespace std;

bool Wordoku::valid(const vector<string> &wordoku_board, const string &letters)
{
    //string used to test individual rows and columns against letters
    string current;

    //for each row
    for (int i = 0; i < 9; i++)
    {
        //set current = the row
        current = wordoku_board[i];
        //sort current so that the letters are in order
        sort(current.begin(), current.end());
        //test current against letters, return 0 if test fails
        if (current != letters)
            return 0;
    }

    //potential cout for debugging
    //cout << "Rows are checked." << endl;

    //for each column
    for (int k = 0; k < 9; k++)
    {
        //give current an initial value
        current = wordoku_board[0][k];
        //for each row starting at row 2
        for (int l = 1; l < 9; l++)
            //add the element to current
            current += wordoku_board[l][k];
        //sort current so the letters are in order
        sort(current.begin(), current.end());
        //test current against letters, return 0 if test fails
        if (current != letters)
            return 0;
    }

    //potential cout for debugging
    //cout << "Columns are checked." << endl;

    //string array to test 3x3 nodes of board
    string square[9];

    //for each node
    for (int j = 0; j < 9; j++)
    {
        //if one of the first 3 nodes
        if (j < 3)
            //give node j its first value
            square[j] = wordoku_board[0][j*3];
        //if one of the second 3 nodes
        else if (j >= 3 && j < 6)
            //give node j its first value
            square[j] = wordoku_board[3][(j-3)*3];
        //if one of the last 3 nodes
        else
            //give node j its first value
            square[j] = wordoku_board[6][(j-6)*3];
    }

    //for each node
    for (int m = 0; m < 9; m++)
    {
        //for each value in node m starting at the second value
        for (int n = 1; n < 9; n++)
        {
            //if one of the first 3 nodes
            if (m < 3)
            {
                //if one of the first 3 values
                if (n < 3)
                    //append the desired value to node m
                    square[m] += wordoku_board[0][n+(m*3)];
                //if one of the second 3 values
                else if (n >= 3 && n < 6)
                    //append the desired value to node m
                    square[m] += wordoku_board[1][(n-1)+(m*3)];
                //if one of the last 3 values
                else
                    //append the desired value to node m
                    square[m] += wordoku_board[2][(n-1)+(m*3)];
            }
            //if one of the second 3 nodes
            else if (m >= 3 && m < 6)
            {
                //if one of the first 3 values
                if (n < 3)
                    //append the desired value to node m
                    square[m] += wordoku_board[3][n+((m-3)*3)];
                //if one of the second 3 values
                else if (n >= 3 && n < 6)
                    //append the desired value to node m
                    square[m] += wordoku_board[4][(n-1)+((m-3)*3)];
                //if one of the last 3 values
                else
                    //append the desired value to node m
                    square[m] += wordoku_board[5][(n-1)+((m-3)*3)];
            }
            //if one of the last 3 nodes
            else
            {
                //if one of the first 3 values
                if (n < 3)
                    //append the desired value to node m
                    square[m] += wordoku_board[6][n+((m-6)*3)];
                //if one of the second 3 values
                else if (n >= 3 && n < 6)
                    //append the desired value to node m
                    square[m] += wordoku_board[7][(n-1)+((m-6)*3)];
                //if one of the last 3 values
                else
                    //append the desired value to node m
                    square[m] += wordoku_board[8][(n-1)+((m-6)*3)];
            }
        }
    }

    //for each node
    for (int r = 0; r < 9; r++)
    {
        //sort the node so the letters are in order
        sort(square[r].begin(), square[r].end());
        //test the node against letters, return 0 if the test fails
        if (square[r] != letters)
            return 0;
    }

    //potential cout for debugging
    //cout << "All 3 criteria have been met, board is a viable wordoku board." << endl;

    //wordoku board has matched all criteria, return 1
    return 1;
}

任何帮助或建议将不胜感激,在此先感谢!

4

1 回答 1

1

std::vector并且std::stringstd命名空间的成员。

在您的标题中,您忘记了:

bool valid(const std::vector<std::string> &wordoku_board, const std::string &letters);
//               ^^^^^       ^^^^^                              ^^^^^

在你的源文件中没问题,因为你做了using namespace std;. 不要在头文件中这样做,这是一种不好的做法

PS:我在我的电脑上尝试了你的代码并添加std::解决了所有问题。

于 2013-09-05T18:10:02.323 回答