I'm very very new to c++.I'm truing to built a program to make new appointment etc.

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Appointment;

class MyDate
{
      private:
             string date;
             string hour;
             
      public:           
             
             MyDate();//constructor xoris parametrous//
             
             
             void set_date(string _date)
                  {
                     date=_date;
                     } 
                     
             void set_hour(string _hour)
                  {
                        hour=_hour;
                       }                            
                                  
                     
                     
            friend void print(Appointment &,MyDate &)    ;     
            friend class Scedule;
                     
                     };                          
class Appointment
{
      private: 
      string description;
      string date;
      string hour;         
      public:
             
             
         Appointment();//constructor xoris parametrous//        
                                              
                            
            void set_description(string _description)
            {
                 description=_description;
                 }                
                            
                   
                   friend class MyDate;
                   friend class Scedule;
                   
               friend void print(Appointment &, MyDate &);
                                     
            };

         void print(Appointment &a,MyDate &d)
               {
                    cout<<"description of appointment "<<a.description<<endl;
                    cout<<"date "<<d.date<<"  hour "<<d.hour<<endl;
                    } 
                                                          
                    
            
    class Scedule
{
  vector<Appointment> m_apps;

public:
  void addAppointment(Appointment const & app)
  {
     m_apps.push_back(app);
  }
                               
                     
                   
friend class Appointment;
friend class MyDate;
};     


int main(int argc, char *argv[])
{
    Appointment app;
    MyDate dt;
    
  string tmpDesc;
  string tmpDate;
  string tmpHour;
  
  cout<<"Give description of appointment ";
    cin>>tmpDesc;
    cout<<"Give date of appointment  ";
    cin>>tmpDate;
    cout<<"Give hour of appointment  ";
    cin>>tmpHour;
      
    app.set_description(tmpDesc);
    dt.set_date(tmpDate);
    dt.set_hour(tmpHour);
    print(app,dt);
    system("pause");
    return 0;
}

the error is [Linker error] undefined reference to `Appointment::Appointment()'
[Linker error] undefined reference to `MyDate::MyDate()'

i'm sure that i have more things to do to this code,but now i would like your help to solve this error!

Dani AI

Generated

The linker messages mean the classes declare constructors but no definitions were ever provided (so the compiler accepted the declarations, then the linker could not find the function bodies). correctly pointed out that implementation code is needed; 's suggestion to remove the explicit prototypes is also a valid quick fix if no custom initialization is required.

Three practical fixes (pick one):

  • Remove the explicit MyDate() and Appointment() declarations so the compiler generates implicit default constructors (quickest when no special initialization is needed).
  • Provide simple definitions (either empty or with initialization) in the same source file or in a separate .cpp file. Example (works on all C++ versions):
MyDate::MyDate()  { }
Appointment::Appointment() { }
  • Use C++11 defaulting if a modern compiler is available:
MyDate::MyDate() = default;
Appointment::Appointment() = default;

If definitions live in a separate .cpp file, make sure that file is actually compiled and linked. For a command-line build the pattern is:

g++ main.cpp mydate.cpp appointment.cpp -o app

or add every .cpp to the project in an IDE (Code::Blocks/Visual Studio).

Extra tips and checks: confirm the definition signatures exactly match the declarations (typos or differing parameter lists cause unresolved symbols). Consider making the free print function take const references if it only reads data. For multi-word descriptions prefer std::getline(std::cin, tmpDesc) instead of operator>>. Finally, if later initialization is needed, implement constructors to set members or use member initializers rather than leaving everything default-initialized.

Recommended Answers

All 4 Replies

The errors mean that you failed to write the implementation code for those functions.

and how can i fix this?

How?? Simple -- write the implementation code

class MyClass
{
public:
   MyClass()
   {
        cout << "Hello\n";
   {

Or if you don't want to or can't do it inline as above then put the code in a *.cpp file

class MyClass
{
public:
   MyClass();
   // blabla
};

// MyClass.cpp
#include "MyClas.h"

MyClass::MyClass()
{
   cout << "Hello\n";
}

You can also have empty constructors

class MyClass
{
public:
   MyClass() {}; // <<< empty constructor
}

You could also take out the prototypes for those constructors.

commented: good point :) +36
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.