LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [C++] What is wrong with my constructor? (https://www.linuxquestions.org/questions/programming-9/%5Bc-%5D-what-is-wrong-with-my-constructor-4175689766/)

dedec0 02-02-2021 06:39 PM

[C++] What is wrong with my constructor?
 
Hello,

I am having a problem with a basic C++. I have searched around, but i only found reasons pretty different from what my code is. I have also reduced it to a minimal code that reproduces the error.

The gcc error message is:

Code:

t.cpp:15:5: error: expected unqualified-id before ‘{’ token
My code uses 2 files. You can see a partial screenshot of them in Vim here:

https://images2.imgbox.com/3a/59/WhjTFCbD_o.png

Help the image service: https://imgbox.com/WhjTFCbD

Want to read and copy here? Fine. file t.cpp is:

Code:

#include <iostream>
#include <cstdlib>
using namespace std;

#include "t.h"

class Agent
{
    public:
    enum e { a,b,c,d };
    int z[16];
    int n;

    Agent
    {
      int i;
      for( i=0; i<16; i++)
        z[i] = 0;
    }
};

file t.h is:

Code:

#ifndef tH
#define tH

#endif

So, what is wrong with my code? I tried to do a few changes with it, but this error never disappeared, except when something i know is wrong was written. What is missing or wrong in the class constructor? What is an "unqualified-id"?

Unrelated question: why i do not have gcc manual page, having Debian gcc installed?? I checked files installed with its package, and it has other man pages, but not of "gcc" itself. Years ago it had!

dugan 02-02-2021 07:06 PM

The "()" in a constructor definition is not optional. So it should be:

Code:

  Agent()
    {
      int i;


dedec0 02-02-2021 07:24 PM

LOL... how did i miss that?? I even looked a book teaching C++, compared with my program, and nothing...


All times are GMT -5. The time now is 11:15 PM.