Find the answer to your Linux question:
Results 1 to 5 of 5
Hi All, I am getting this error while compiling a c++ code. The error is: " ISO C++ forbids declaration of disp1 with no type ". Inside the code I ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    1

    ISO C++ forbids declaration of disp1 with no type

    Hi All,

    I am getting this error while compiling a c++ code.
    The error is: "ISO C++ forbids declaration of disp1 with no type".
    Inside the code I wrote a simple template. The code is like:
    Code:
    #include<iostream>
    using namespace std;
    
    template<class T>
    class Data{
    public:
    T t1, t2;
    disp1(T v1, T v2)
    {
    t1=v1;
    t2=v2;
    cout<<t1<<'\t'<<t2<<endl;
    }
    Data(){}
    ~Data(){}
    };
    int main()
    {
    Data<int> d_int;
    d_int.disp1(11,12);
    
    return 0;
    }
    Last edited by devils casper; 04-02-2008 at 07:40 PM. Reason: Added [code]...[/code] tag.

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    you need to either declare a return type for the function or make it void

    Code:
    void disp1 {
    }

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    For that particular function, I'd vote for void, because the function as posted doesn't seem to want to return anything useful.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This is true of every function in C++, with two exceptions: constructors and destructors. These functions (recognizable because they have the name of the class) do not have a return type. Every other function must.

    In C (before C99 at least), you are allowed to omit the return type of a function, and it assumed to be int. However, C++ does not allow this.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by Cabhan View Post
    This is true of every function in C++, with two exceptions: constructors and destructors. These functions (recognizable because they have the name of the class) do not have a return type. Every other function must.

    In C (before C99 at least), you are allowed to omit the return type of a function, and it assumed to be int. However, C++ does not allow this.
    Yep. C++ is far more strict in that regard.

    There might be some compilers that allow this, but I always vote for portable code. And since the standard forbids that, it's not portable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...