LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2021, 06:39 PM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Rep: Reputation: 51
Question [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!
 
Old 02-02-2021, 07:06 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

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

Code:
   Agent()
    {
       int i;
 
2 members found this post helpful.
Old 02-02-2021, 07:24 PM   #3
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Red face

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


Reply

Tags
c++, cpp, gcc



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructor takes string => constructor not called...? (C++) JohnGraham Programming 4 02-16-2012 12:37 PM
C++ constructor/copy constructor issues in g++ 3.4.6 abhinav.dube15 Programming 7 03-13-2011 08:27 AM
Class constructor not being called ChimpFace9000 Programming 1 06-03-2002 08:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration