Meaning Of A Error Sentense

This is a discussion on Meaning Of A Error Sentense within the C++ Programming forums, part of the General Programming Boards category; I got an Error when i am compiling a program i know wut's wrong with it,and i have correct it ...

  1. #1
    Q4u
    Q4u is offline
    Registered User
    Join Date
    Jul 2002
    Posts
    4

    Meaning Of A Error Sentense

    I got an Error when i am compiling a program
    i know wut's wrong with it,and i have correct it back,
    however,i dont understand the error msg that it show

    "pure specifier can only be specified for functions"

    wut does this sentense means?

    Thx for helps!

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Show some code. You've probably got an error around a pure virtual function somewhere but without seeing the code its very hard to tell.

  3. #3
    Q4u
    Q4u is offline
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    Here's the code:
    #include <iostream.h>

    class ctry
    {
    public:
    int x=12;
    };

    int main()
    {
    ctry test1;
    cout << test1.x << "\n";
    return 0;
    }

    i know where the problem is,i shouldnt assign the value for x,right?
    but i just want to know wut
    "pure specifier can only be specified for functions"
    means

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    class ctry
    {
    public:
    int x=12;
    };
    If you try to assign a value to something in a class the compiler will think you want it to be pure which is only allowed for functions. Even then you should also get an error that only =0 is allowed as a pure specifier. What you want to do is provide a constructor that assigns the value to x when the object is created.
    Code:
    #include <iostream>
    using namespace std;
    
    class ctry 
    { 
    public:
      ctry(){ x = 12; };
      int x; 
    };
    
    int main() 
    { 
      ctry test1; 
      cout << test1.x << "\n"; 
      return 0; 
    }

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    One other point, use code tags when posting code. Small snippets are easy to read but naything longer than a few lines is almost impossible.

  6. #6
    Unregistered
    Guest
    Code:
    #include <iostream>
    using namespace std;
    
    class ctry 
    { 
    public:
      ctry(){ x = 12; };
      int x; 
    };
    
    int main() 
    { 
      ctry test1; 
      cout << test1.x << "\n"; 
      return 0; 
    }
    If you try to assign a value to something in a class the compiler will think you want it to be pure which is only allowed for functions. Even then you should also get an error that only =0 is allowed as a pure specifier. What you want to do is provide a constructor that assigns the value to x when the object is created.
    Wut do u mean by "Pure"?

    Even then you should also get an error that only =0 is allowed as a pure specifier.
    Then why inside the constructor,u assign the x to be 12
    but not =0?

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    A pure function in a class means that that class is abstract and no objects of it can be made, it can only be inherited. In the class declaration you can only assign a value when you want a function to be pure and that value has to be 0.
    Code:
    void function() =0;
    Assigning 12 to a member of the class in the constructor and declaring a function to be pure virtual are two unrelated things. You can assign values to members in the methods, but not in the class declaration
    Code:
    class test
    {
    public:
      int x = 0 // NO!
      int y;
      void setY() { y = 0; }; // Yes
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is the meaning of "offset"
    By cromologic in forum C Programming
    Replies: 5
    Last Post: 05-02-2008, 10:09 AM
  2. the meaning of " >> "
    By arian in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2005, 10:40 AM
  3. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 08:53 PM
  4. would you help me with Linked list, please?
    By unhwan in forum C Programming
    Replies: 1
    Last Post: 06-11-2002, 01:24 AM
  5. WINAPI: Meaning of HDC ?
    By Mecnels in forum Windows Programming
    Replies: 1
    Last Post: 01-21-2002, 10:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21