Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone help me understand this error? "definition of implicitly-declared ‘classA::classA()’"

Tags:

Heres the code:

#include <cstdlib> #include <iostream> using namespace std;  class classA {                          protected:                 void setX(int a);        private:               int p; };  classA:: classA() {                      //error here.  p = 0; }  void classA:: setX(int a) {  p = a;     }  int main() {        system("PAUSE");     return EXIT_SUCCESS; } 
like image 560
darko Avatar asked Apr 23 '11 17:04

darko


People also ask

When a destructor is implicitly declared and implicitly defined for a class?

Destructors are implicitly called when an automatic object (a local object that has been declared auto or register , or not declared as static or extern ) or temporary object passes out of scope. They are implicitly called at program termination for constructed external and static objects.

Is private within this context?

The "private within this context" error refers to the fact that the functions addShipment , reduceInventory and getPrice are not members or friends of the class Product , so they cannot use its private members.


2 Answers

You forgot to declare the constructor in the class definition. Declare it in public section of the class (if you want clients to create instance using it):

class classA {        public:                classA();    // you forgot this!              protected:                 void setX(int a);        private:               int p; }; 

Now you can write its definition outside the class which you've already done.

like image 140
Nawaz Avatar answered Oct 04 '22 05:10

Nawaz


class classA {                          protected:                 classA(); // you were missing an explicit declaration!                 void setX(int a);        private:               int p; };  classA:: classA() {   p = 0; } 
like image 27
Johannes Schaub - litb Avatar answered Oct 04 '22 04:10

Johannes Schaub - litb