2d Array Help

Pages: 12
Currently getting an error "invalid use of tempalte-name 'std::vector' without an argument list.


Well the posted code doesn't use templates... So post the actual code that is causing the problem and the reported error(s).
vector< vector<int> > fill( const vector<string> &board )
whoops! this line. Trying to figure out the correct way to put it into a class with the parameters.
OK, here's an equivalent OOP version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <utility>
using namespace std;

//----------------------------------------------------------

class Game
{
   vector<string> board;
   vector< vector<int> > F;
public:
   void readInput( istream &in );
   void fill();
   void drawSolution( int W = 1 );
};

//--------------

void Game::readInput( istream &in )
{
   board.clear();
   for ( string line; getline( in, line ); ) board.push_back( line );
}

//--------------

void Game::drawSolution( int W )
{
   for ( auto &row : F )
   {
      for ( int e : row )
      {
         if ( e < 0 ) cout << setw( W ) << '.';
         else         cout << setw( W ) << e;
      }
      cout << '\n';
   }
}

//--------------

void Game::fill()
{
   const vector< pair<int,int> > jp = { {-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,1}, {1,-1}, {1,0}, {1,1} };

   int rows = board.size(), cols = board[0].size();
   queue< pair<int,int> > Q;
   F = vector< vector<int> >( rows, vector<int>( cols, -99 ) );

   // Set starting points (as 0) and barriers (as -1)
   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ )
      {
         if ( board[i][j] == 'A' )
         {
            F[i][j] = 0;
            Q.push( { i, j } );
         }
         else if ( board[i][j] == 'X' )
         {
            F[i][j] = -1;
         }
      }
   }

   // Check adjacent points to current front-of-queue
   while( !Q.empty() )
   {
      auto pr = Q.front();   Q.pop();
      int i = pr.first, j = pr.second;
      int nextValue = F[i][j] + 1;
      for ( int n = 0; n < jp.size(); n++ )
      {
         int ii = i + jp[n].first, jj = j + jp[n].second;                // adjacent point
         if ( ii < 0 || ii >= rows || jj < 0 || jj >= cols ) continue;   // if outside domain then ignore
         if ( F[ii][jj] < -1 )                                           // not yet set
         {
            F[ii][jj] = nextValue;                                       // set or improve value
            Q.push( { ii, jj } );                                        // add to current front line
         }
      }
   }
}

//----------------------------------------------------------

int main()
{
// ifstream in( "input.txt" );              // for real
   istringstream in( "..............\n"     // for demo
                     "..............\n"
                     "..............\n"
                     "..............\n"
                     ".......X......\n"
                     ".......X......\n"
                     "...A...X......\n"
                     "...A...X......\n"
                     ".......X......\n"
                     ".......X......\n" );
                     
   Game G;
   G.readInput( in );
   G.fill();
   G.drawSolution( 3 );
}


  6  6  6  6  6  6  6  6  6  6  7  8  9 10
  5  5  5  5  5  5  5  5  5  6  7  8  9 10
  4  4  4  4  4  4  4  4  5  6  7  8  9 10
  3  3  3  3  3  3  3  4  5  6  7  8  9 10
  3  2  2  2  2  2  3  .  5  6  7  8  9 10
  3  2  1  1  1  2  3  .  6  6  7  8  9 10
  3  2  1  0  1  2  3  .  7  7  7  8  9 10
  3  2  1  0  1  2  3  .  8  8  8  8  9 10
  3  2  1  1  1  2  3  .  9  9  9  9  9 10
  3  2  2  2  2  2  3  . 10 10 10 10 10 10



Your pal Juan probably needs a similar code:
https://cplusplus.com/forum/general/284517/#msg1232240
Last edited on
This has been working fairly well. Quick question - programming industry wise is OOP best practice?
a456df wrote:
programming industry wise is OOP best practice?


I don't work in the "programming industry", so I couldn't advise you. "Best practice" is an awfully subjective opinion. One person's "best practice" is another person's cause to spit.

You have both procedural and object-oriented codes to accomplish your task in this thread. Take whichever you prefer. The nice thing about C++ (as opposed to Java, say) is that both paradigms are available to you. Moreover, they are easily mixed (std::vector<...> is a class, for example, as is std::string).
Last edited on
there are few things worse that bad OOP code. Objects where none are needed have even broken multiple languages; python for example uses a fat heavy object for integers and it is at times an order of magnitude slower than other languages because of it, and java forces useless objects upon the user that do nothing but wrap a simple function in unnecessary gibberish syntax.

OOP when used correctly has untold numbers of ways to make code easier to read and maintain.

a mix of free floating functions and OOP is a happy combination. Unnecessary objects bring nothing but clutter to the table, and code without objects tends to get convoluted and difficult to work with due to excessive parameter passing and frustrations gluing functionality together. Use both in C++, as makes the most sense for the code you are writing.

an example of a function that does not need an object: get a string from the user, validate that it is a number within some range, and return the number or throw an error. That will appear in some 90% of the programs you write after school, somewhere. What use is putting it into an object? There are countless little math and string utility type routines you will need all the time (another example is raise a number to an integer power, where pow() is too slow and sometimes imprecise due to floating point issues). Just let them be free functions.

@a456df, you want to get an idea of what is best practice by a few of the heavy-weight C++ experts spend some time reading and understanding the C++ Core Guidelines:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines

As the title says these are "good idea" guidelines, not "must do it this way" rules.

For example, having using namespace std; in one's code:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-using

See also:
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

There are possible risks involved with having the using namespace std; directive in your code, risks that usually are not mentioned when learning C++.

C++ Core Guidelines, SF6 & SF7:

Read the explanatory text before and after the SF6 sample.

Having the using namespace std; directive in code is not an error. The people who have it in their code presumably know the risks. Do you know what can happen?

It was originally supposed to be used with older C++ code that didn't have standard library namespaces and make it easier to transition that legacy code to newer C++ language versions.

There are now C++ safer ways that are less likely to have risks, using declarations.

https://www.learncpp.com/cpp-tutorial/using-declarations-and-using-directives/

There are a few select features of the C++ stdlib that require a using declaration to activate the desired feature.

A popular 3rd party library, Boost, is multiple namespace structured that not having namespace declarations for the library is a pain with lots of repetitive typing that can get mistyped.

The C++ Core Guidelines are suggestions, not "you must do this" requirements.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#SS-aims

Personally I never have the using directive in my code, ever. I prefer to prefix std:: when using C++ stdlib features. Now it is automatic typing std::. I actually have to stop and think to NOT use it.

I do have using declarations on occasions, though I try to use them very sparingly. On a case by case basis, mostly when using a C++/3rd party library feature buried in a namespace morass.
Topic archived. No new replies allowed.
Pages: 12