-2

I have a series of variables including a struct that is declared in another file (Vek.h). In this file, I also declare a function called correct:

class Vek: public Player{
   protected :
    int counter = 0; 
    struct next {int nexti=0; int nextj=0;};
    next newNext = {0,0};
    int jTemp; 
    int iTemp = 0;
    int jPast;
    int iPast;
    int hits;
    int pastHits = 0;
    int count = 0;
    void putShip(Cell[OCEAN_SIZE][OCEAN_SIZE]);
    int correct(int, int, int, int, int, int, struct);
    // int fire(vector<int>submit);
    Coords shot;

In the second file (Vek.cpp) I try to call this function and pass the struct next as a parameter:

int Vek :: correct(int iTemp, int jTemp, int iPast, int jPast, int hits, int count, struct newNext){
  int attemptsi[2]{iTemp+1,iTemp-1};
  int attemptsj[2]{jTemp+1,jTemp-1};
  int i;
  int j; 
  if(count == 0){
     i = attemptsi[0];
     j = jTemp;

    // Coordinates.j = 5;
  }else if(count == 1){
     i = attemptsi[1];
     j = jTemp;
    // Coordinates.j = 5;
  }else if(count == 2){
     i = iTemp; 
     j = attemptsj[0];
    // Coordinates.j = 5;
  }else if(count == 3){
     i = iTemp;
     j = attemptsj[1];
    // Coordinates.j = 5;
  }
  next.nexti = i; 
  next.nextj = j; 


  count++;
  return next; 
}

I'm getting an error calling correct due to the struct. Among others I get the error:

out-of-line definition of 'correct' does not match any declaration in 'Vek'

What is going on here? How can I pass the struct next to the correct function?

Without spaces around correct I get the error:

declaration of anonymous struct must be a definition

Mark13Vek
  • 1
  • 1
  • 1
    I'd suggest expressing this as `int Vek::correct(...)`, no spaces around `::`, to conform with most C++ coding conventions. – tadman Nov 10 '20 at 23:58
  • @tadman ok thanks. I get a new error now, see the above edit – Mark13Vek Nov 11 '20 at 00:00
  • What is `struct next` and why doesn't the variable have a name? I'd expect to see `next x` or `Foo next`. – tadman Nov 11 '20 at 00:01
  • What is `struct`? It should probably be written as `struct Foo next`. The struct name must be specified. – Locke Nov 11 '20 at 00:01
  • `struct` is a keyword, you may need to rename your `struct` object to something different. – Karl Nicoll Nov 11 '20 at 00:01
  • You really don't see the glaring difference between `int correct(int, int, int, int, int, int, struct);`, and what you try to define in your `cpp` file? What do you believe that the lonely `struct` in this declaration mean? – Sam Varshavchik Nov 11 '20 at 00:14
  • @SamVarshavchik sorry - see above. I've now declared newNext as a next struct, and am trying to pass it to correct. Im new to c++ and unclear how structs function exactly – Mark13Vek Nov 11 '20 at 00:28
  • @KarlNicoll sorry, see the above – Mark13Vek Nov 11 '20 at 00:28
  • @Locke sorry, see the above – Mark13Vek Nov 11 '20 at 00:28
  • What is this `struct newNext` all about, suddenly appearing out of nowhere, and why do you believe it has anything to do with `struct next`? Are you aware that in C++ function and method definition must ***match exactly*** the corresponding function and method declarations? For more information [see a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Nov 11 '20 at 00:30
  • @SamVarshavchik https://www.learncpp.com/cpp-tutorial/47-structs/ per this link, I thought you needed to initialize a struct in order to use it. So newNext was my attempt at initializing a struct of type next – Mark13Vek Nov 11 '20 at 00:32
  • As always, it would be nice if you boiled the example down to the absolute minimum. Often, these errors become obvious when all the fluff is gone. – Peter - Reinstate Monica Nov 11 '20 at 00:33
  • Ah, I see. You're trying to learn C++ by reading some web site. Unfortunately, any clown can put together a web site that says anything. Doesn't mean that it's any good, or is correct and helpful. You will not learn C++ by reading some web site or watching a Youtube video. The only way to learn C++ [is by studying a quality C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Sorry, this is not how you go about declaring and then defining class methods, you need to learn this by following examples in a textbook. – Sam Varshavchik Nov 11 '20 at 00:35
  • @SamVarshavchik I don't think I would shell out $79 for a text book these days either. Do you find the tutorial lacking? It didn't seem obviously wrong. But the OP should work on his copying skills ;-). – Peter - Reinstate Monica Nov 11 '20 at 00:39
  • Well, @Peter -- $79 doesn't sound too bad and it doesn't seem that the prices of textbooks changed much since the old days when I learned C++. And back then there was no Stackoverflow or anywhere anyone can run off to, in search of someone else to do their homework for them, so all you had to go by was your textbook and your own wits. Seemed to do the trick, back then, don't see any reason why this still can't work these days. – Sam Varshavchik Nov 11 '20 at 00:43

1 Answers1

0

The definition of a struct consists of

  • the keyword struct
  • typically the (type) name of the struct
  • the struct body, in braces.

Example:

struct    Int_Struct    { int mI; };
  ^           ^              ^ 
keyword      name           body

The name, here Int_Struct, can directly be used as the type name (a difference to C).

The definition of a variable of that type follows the usual convention typename variablename:

Int_Struct myVar; As always, this is also true for the declaration of function parameters:

void f(Int_Struct myParam);

You can, in the prototype, or if the parameter is not used, omit the parameter name and simply write void f(Int_Struct);. But typically a "speaking" parameter name is given so that the reader gets an idea what the parameter is about; the best documentation is self-documentation.

The keyword struct cannot be used as a type name, as you do in with the last parameter in the function definition int Vek :: correct(int iTemp, int jTemp, int iPast, int jPast, int hits, int count, struct newNext): Remember, the parameter declaration has the form typename parametername, which makes struct be the type name which is, as stated, not possible. If you wish you can redundantly use the keyword struct in the C manner as part of the type name in a parameter or variable declaration: struct Int_Struct varName; is equivalent to the more idiomatic and common Int_Struct varName;.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62