Like Tree1Likes
  • 1 Post By iMalc

its an undefined reference but i dont know why

This is a discussion on its an undefined reference but i dont know why within the C++ Programming forums, part of the General Programming Boards category; hello everyone ...first post! got a piece of code that has an error when compiling at this stage: Code: System ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    its an undefined reference but i dont know why

    hello everyone ...first post!

    got a piece of code that has an error when compiling at this stage:

    Code:
    System C_wdm[(channels)];
      for (int i=0;i<(channels);i++){
      cp_ptr[i] = new Field((field_win*2),field_win);
      *cp_ptr[i] = field;
      }
    i get the output:

    Code:
    Main.o(.text+0x10d9): In function `main':
    /home/frenchcr/src/mdwdm/Main.cc:202: undefined reference to `Field::operator=(Field const&)'
    collect2: ld returned 1 exit status
    make: *** [Main] Error 1
    Im sure its a problem with this Main.cc file and not the library as Field is a class in my library and I just tried another Main.cc that i wrote for some other purpose, which also uses the Field class, and it compiled and ran fine.

    the full code is:

    Code:
    #include <gnls.h>
    #include <iostream>
    #include <time.h>
    
    #define GHz 0.001
    
    int main(int argc,char *argv []){
    
      if(argc!=12){
        cout<<"Usage :\n";
        cout<<"mdwdm <Peak_Power> <Pulse_Width> <Chirp> <Innital Freq> <Space> <DeTune> <Filter FileName> <Fgain> <Channels> <# Map>  <QF File Name>\n";
        exit(1);
      }
    
    double peak_power = atof(argv [1]);
      double pulse_width = atof(argv [2]);
      double chirp  = atof(argv [3]);
      //fixed parameters for fibre
      double D=8.0;
      char* filterName=argv[7];
      double S=0.057;
      double loss=0.205;
      double length=50;
      double RD=-16;
      double RS=-0.116;
      double Rloss=0.28;
      double Rlength=25;
      //fixed parameters for signal
      double seperation=25; //for 40Gbit/s
      double num_of_bits=128;
      int field_win=(seperation*num_of_bits)/2;
      double phase=0;
      //Frequency
      double freq=0.0;
      double init_freq=atof(argv [4]);
      double space=atof(argv [5]);
      double f_detune= atof(argv [6]);
      double fgain= atof(argv [8]);
      int channels=atoi(argv [9]);
      int num_map=atoi(argv [10]);
      //Aplifier
      double ampgain=length*loss+Rlength*Rloss;
      double noise=1.5;
      //Qfactor
      ofstream qf_data_stream[(channels)],sample_stream[(channels)],eye_stream[(channels)];
      double cutoff=1000/(seperation*1000);
      int fp=0;
      char num[10];
      char ind[20];
      string qf_file,qf_buff=(argv[11]); //Change
      string eye_file,eye_buff="eye";
      string sample_file,sample_buff="sample";
      //bool minuse_detune=true;
      fgain=0;
    
    //********
      long int seed=0;
      seed=time(0);
      srand48(seed);
    //****************
    
      Loop::break_on_interrupt();
      Fibre::verbosity=true;
      Fibre::unit_system=Fibre::ps_per_nm_per_km;
    
      Field field((field_win*2),field_win);
      Field *cp_ptr[channels];
    
      Fibre Teralight(0,D,S,0,1.55,loss,65);
      Fibre RTeralight(0,RD,RS,0,1.55,Rloss,25);
      OutputUnit wdmtera("multiwdm_filt",field);
    
    //*******Opening files for eye diagram and q_factor
        for(fp=0;fp<(channels);fp++){
        //convert int to string and put into  num
        sprintf(num,"%d",fp);
        sprintf(ind, "-%d_%.1f_%.1f",(int)pulse_width,(space*1000),f_detune*1000);
        qf_file=qf_buff+ind+"_C"+num+".dat";
        eye_file=eye_buff+ind+"_C"+num+".dat";
        sample_file=sample_buff+ind+"_C"+num+".dat";
    
        qf_data_stream[fp].open(qf_file.c_str());
        cout<<"Opened File: "<<qf_file<<"\n";
        qf_file.erase(1);
        eye_file.erase(1);
        sample_file.erase(1);
      }
    
      //*************GT Gaussian transmitter*****************
      if(channels%2==0)freq=init_freq-(space*0.5+(channels/2-1)*space);
      	else freq=init_freq-(channels-1)/2*space;
      Add G_Pulse;
      for(int i=0;i<channels;i++){
        cout<<"Frequncy channel A: "<<freq<<"\n";
        double posn=-drand48()*num_of_bits*seperation*0.5;
        cout<<"Signal position + "<<-posn<<endl;
        G_Pulse.add_system(
          Gaussian(pulse_width,peak_power,posn,0,0,chirp)
            +GeneratePattern(PRBS(field,7,seperation,0,freq,phase))
    	+OutputField(wdmtera)
    	+ReadFilter(filterName,(freq-f_detune))
        );
    
        freq=freq+space;
      }
    
    
      //************Transmitter=GT+GF*************************
      System Tx;
      Tx=  G_Pulse +OutputField(wdmtera)
      ;
    
      //*************TeraLight + RTL Fibre********************
    
      Add TLFibre;
      TLFibre.add_system(
          FibreSegment(Teralight,length)+
          FibreSegment(RTeralight,Rlength)+
          Amplifier(ampgain,noise,1.55)+
          OutputField(wdmtera)
      );
    
      Add RTLFibre;
      RTLFibre.add_system(
          FibreSegment(RTeralight,Rlength)+
          FibreSegment(Teralight,length)+
          Amplifier(ampgain,noise,1.55)+
          OutputField(wdmtera)
      );
    
      //*************System GF+GF+TLfibre*********************
      Tx.propagate(field);
      System mwdmsys;
    
       for(int k=1;k<=num_map;k++){
    
    	mwdmsys=TLFibre+RTLFibre;
    	FixEnergy Att(mwdmsys);
    	Att.propagate(field);
      }
    
      //*************DeMutiplex Field and applie Qfac********
    
      System C_wdm[(channels)];
      for (int i=0;i<(channels);i++){
      cp_ptr[i] = new Field((field_win*2),field_win);
      *cp_ptr[i] = field;
      }
    
      //Open Qfactor Files
    
      if(channels%2==0)freq=init_freq-(space*0.5+(channels/2-1)*space);
      	else freq=init_freq-(channels-1)/2*space;
      fp=0;
    
      for(fp=0;fp<(channels);fp++){
    
          C_wdm[fp]=
            ReadFilter(filterName,(freq-f_detune))+
            QFactor(RZ,qf_data_stream[fp],(freq),10,cutoff*1.0,seperation,64)
            +OutputField(wdmtera)
    	;
    
          freq=freq+space;
    
          C_wdm[fp].propagate(*cp_ptr[fp]);
          }
    
      //*************Closing all QFac Files*******************
      //Close all QFACTOR files and cleanup
      cout<<"Closing all Qfactor files: \n";
      for(fp=0;fp<(channels);fp++){
        qf_data_stream[fp].close();
        cout<<"Closing File Number: "<<fp<<"\n";
      }
    
      for (int i=0;i<(channels);i++){
      delete cp_ptr[i];
      }
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    This is a linker error, ensure that you link proper binaries.
    I never put signature, but I decided to make an exception.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    how do i go about fixing an 'undefined reference linker error'.
    i dont understand what steps i should take from here.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    The problem is with your Field class. This main.cpp just happens to expose it.
    Specifically your Field class has no assignment operator and yet it is not declated private.
    It is easy to fix. First ask yourself the question, should instances of the Field class be copyable?

    If yes... Implement the copy-constructor, or otherwise make changes to the class such that it does not block the compiler from auto-generating this on its own, depending on whether it is required for "the rule of three" or not.

    If no... Define the assignment operator and copy constructor of the Field class as private, but still leave them unimplemented. This has the effect that if you use either of these by mistake (which you are currently doing somewhere) that instead of a linker error, you'll get a compile error showing you exactly which line the problem is on.
    Salem likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference
    By ThatDudeMan in forum C Programming
    Replies: 5
    Last Post: 10-20-2010, 12:20 PM
  2. undefined reference to
    By diego in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2010, 03:06 PM
  3. Undefined reference
    By TheEngineer in forum C Programming
    Replies: 17
    Last Post: 08-12-2009, 10:34 AM
  4. Undefined Reference to..
    By PatoAqp in forum C++ Programming
    Replies: 13
    Last Post: 06-11-2009, 09:36 PM
  5. undefined reference
    By siubo in forum C Programming
    Replies: 1
    Last Post: 04-11-2003, 05:34 AM

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