0

its been a couple years since I programmed with C++. My class code compiles but my main comes up with the error:

/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

Here is my code:

main.cc

#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

Wordoku.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;
}

Any help or advice would be greatly appreciated, thanks in advance!

1 Answer 1

1

std::vector and std::string are members of the std namespace.

In your header, you forgot:

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

It is ok in your source file because you did using namespace std;. Don't do this in your header file, it is a bad practice.

PS: I tried your code on my computer and adding std:: solved everything.

Sign up to request clarification or add additional context in comments.

4 Comments

I made the changes and am getting the same error. I'm using putty and the command g++ main.cc to compile.
@user2751738 Don't forget to add Wordoku.cc to the compilation line: g++ main.cc Wordoku.cc.
@user2751738 It just worked for me on my BSD... Is it the exact same error?
Solved, old version also was missing Wordoku:: before function name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.