Thread: Never seen an error like this before

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    15

    Never seen an error like this before

    I'm relatively new to programming, and when I first compiled this program I got a whole slue of errors and slowly whittled it down to the last outstanding error which was for line 93, I had
    Code:
    fin.open(file_name)
    and fixed it by making it
    Code:
    fin.open("file_name")
    but when I tried to compile it after that I got an error that I can't make any sense of. the error reads:
    Code:
    /usr/local/gcc/lib/gcc/sparc-sun-solaris2.10/4.5.2/crt1.o: In function `.nope':
    /usr/local/src/gnu/gcc/gcc-4.5.2/gcc/config/sparc/sol2-c1.asm:94: undefined reference to `main'
    collect2: ld returned 1 exit status
    I don't have a function '.nope' and i'm not sure what the undefined reference to the main 'main' is given that I haven't even crated a main file yet. Can anyone help me out with this?

    Code:
    #include <iostream>#include <cstdlib>
    #include <fstream>
    #include <string>
    #include "MyTime.h"
    using namespace std;
    
    
    MyTime::MyTime(){
    
    
      hours = 0;
      minutes = 0;
      simplify();
    
    
    }
    
    
    MyTime::MyTime(int& time1, int& time2){
    
    
      time1 = hours;
      time2 = minutes;
    
    
    }
    
    
    void MyTime::set_time(int time1, int time2){
    
    
      hours = 0;
      minutes = 0;
      simplify();
    
    
    }
    
    
    int MyTime::get_time1()const{
    
    
      return hours;
    
    
    }
    
    
    int MyTime::get_time2()const{
    
    
      return minutes;
    
    
    }
    
    
    istream& operator >> (istream& ins, MyTime t1){
    
    
      char selection1, selection2;
      string file_name;
      ifstream fin;
      int num1 = 0, num2 = 0, tmp1 = 0, tmp2 = 0;
    
    
      cout << "Do you want to input the number of hours and minutes now or from a file? (enter N for now and F for file): ";
      cin >> selection1;
    
    
      while (selection1 != 'n' && selection1 != 'N' &&
          selection1 != 'f' && selection1 != 'F'){
        cout << "Please enter a valid selection.  N or F: ";
        cin >> selection1;
      }
    
    
      if (selection1 == 'n' || selection1 == 'N'){
        cout << "Enter the number of hours: ";
        ins >> t1.hours;
    
    
        cout << "Enter the number of minutes: ";
        ins >> t1.minutes;
      }
    
    
      else if (selection1 == 'f' || selection1 == 'F'){
        cout << "Please enter the name of the file that has the number of minutes and hours: ";
        cin >> file_name;
    
    
        fin.open("file_name");
        if (fin.fail()){
          cout << "The file " << file_name << " does not exsist in this folder, please put the file into this folder " << endl
               << " or hole CRTL + C to close out of this program: ";
          cin >> file_name;
        }
    
    
        while (fin >> num1 >> num2){
          tmp1 += num1;
          tmp2 += num2;
        }
    
    
        cout << "Your file has a total of " << tmp1 << " hours and " << tmp2 << " minutes.  If this is backwards and the totals " << endl
             << "should be " << tmp2 << " hours and " << tmp1 << " minutes and you would like the correct this please type Y, and if you "<< endl
             << "do not wish to correct this type N: ";
        cin >> selection2;
    
    
        while (selection2 != 'y' && selection2 != 'Y' &&
               selection2 != 'n' && selection2 != 'N')
          cout << "Please enter a valid selection, Y or N: ";
          cin >> selection2;
    
    
        if (selection2 == 'n' || selection2 == 'N'){
          t1.hours = tmp1;
          t1.minutes = tmp2;
          ins >> t1.hours >> t1.minutes;
        }
    
    
        else if (selection2 == 'y' || selection2 == 'Y'){
          t1.hours = tmp2;
          t1.minutes = tmp1;
          ins >> t1.hours >> t1.minutes;
        }
      }
    
    
    
    
      return ins;
    
    
    }
    
    
    void MyTime::simplify(){
    
    
      int tmp1, tmp2;
    
    
      if (minutes >= 60){
        tmp1 = minutes / 60;
        tmp2 = minutes % 60;
        hours += tmp1;
        minutes += tmp2;
      }
    
    
    }
    Last edited by pepperlizard; 11-26-2012 at 04:02 PM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Change
    Code:
    fin.open(file_name);
    to
    Code:
    fin.open(file_name.c_str());
    Open accepts a const char* as the first parameter. Your file_name is a std::string object, so you must pass the c-string in to the open function, and this is achieved by the c_str member function.
    Last edited by twomers; 11-26-2012 at 04:01 PM.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    I understand now what you are saying, however it did not resolve the problem, the error is still there.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    don't have a function '.nope' and i'm not sure what the undefined reference to the main 'main' is given that I haven't even crated a main file yet. Can anyone help me out with this?
    If you don't have a function called main, you can't compile the program. Every C and C++ program needs a main function, no exceptions. The error you are getting is not a compiler error, it is a linker error. This is probably why it looks abit strange to you.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Quote Originally Posted by Neo1 View Post
    If you don't have a function called main, you can't compile the program. Every C and C++ program needs a main function, no exceptions. The error you are getting is not a compiler error, it is a linker error. This is probably why it looks abit strange to you.
    that did it, I didn't think that I needed a main program to run compile the program with the function definitions, but i have been wrong before, haha. thanks guys

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The process of building an executable is a two-step process, compilation of source files into object files which is then followed by a linking step which combines all the compiled object files - plus additional libraries - into a final executable. You can certainly compile a source file into an object file without a main function (main is a function not a program). The issue comes when you try to build the executable and the linker needs to find a main function as the executable's starting point. You may have been trying to build (which would involve the second linking step) instead of just simply attempting to compile.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2012, 03:33 AM
  2. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM