Как создать объектный файл из .cpp с заголовочным файлом - PullRequest
0 голосов
/ 06 ноября 2019

Я пытаюсь создать объектный файл, используя g ++, набрав в терминале linux:

user$ g++ -c clockType_Cayas.cpp

clockType_Cayas.h

/* File: clockType_Cayas.h
 * Author: Von Vic Cayas
 * Date: October 27, 2019
*/
#ifndef clockType
#define clockType
class clockType {
public:
    // Set hours, minutes, or seconds
    void setHours(int hours);
    void setMinutes(int minutes);
    void setSeconds(int seconds);

    // Get hours, minutes, or seconds
    int getHours();
    int getMinutes();
    int getSeconds();

    // Get elapsed time and remaining time
    int getElapsedTime();
    int getRemainingTime();

    void compareClock(clockType& otherClock);

    clockType();
    clockType(int hours, int minutes, int seconds);
private:
    int hr;
    int min;
    int sec;
};

#endif

clockType_Cayas.cpp

/* File: clockType_Cayas.cpp
 * Author: Von Vic Cayas
 * Date: October 27, 2019
*/
#include <cmath>
#include <iostream>
#include "clockType_Cayas.h"
using namespace std;
// SETTERS
// #####################################################
// For clockType, sets hr, min, and sec private variables
void clockType::setHours(int hours) {hr = hours;}
void clockType::setMinutes(int minutes) {min = minutes;}
void clockType::setSeconds(int seconds) {sec = seconds;}
// #####################################################


// GETTERS
// #####################################################
// For clockType, returns hr, min, and sec private variables
int clockType::getHours() {return hr;}
int clockType::getMinutes() {return min;}
int clockType::getSeconds() {return sec;}

// For clockType, returns total elapsed time in seconds
int clockType::getElapsedTime() {return hr*60*60 + min*60 + sec;}

// For clockType, returns total remaining time in seconds
int clockType::getRemainingTime() {return 24*60*60 - getElapsedTime();}
// #####################################################

// Compares two objects of clockType, comparing their total elapsed time
void clockType::compareClock(clockType& otherClock) {
    float getRemainder(float a);

    float diff = abs(otherClock.getElapsedTime() - getElapsedTime()); // Get difference b/w clocks in seconds

    // Conversion from decimal seconds to time
    float hours, minutes, seconds;

    hours = diff / (60*60);
    minutes = getRemainder(hours) * 60;
    seconds = getRemainder(minutes) * 60;

    hours = static_cast<int>(hours);
    minutes = static_cast<int>(minutes);
    seconds = static_cast<int>(seconds);

    cout << hours << ":" << minutes << ":" << seconds << endl;
}

float getRemainder(float a) {
    while (a > 1) {
        a -= 1;
    }
    return a;
}

// Constructors
// #####################################################
clockType::clockType() {
    hr = 0;
    min = 0;
    sec = 0;
}

clockType::clockType(int hours, int minutes, int seconds) {
    hr = hours;
    min = minutes;
    sec = seconds;
}
// #####################################################

Однако, когда я пытаюсь создать объектный файл, я получаю несколько ошибок, которые я не слишком уверен, что может быть причиной

Терминал

In file included from clockType_Cayas.cpp:7:
clockType_Cayas.h:23:29: error: expected identifier before ‘&’ token
  void compareClock(clockType& otherClock);
                             ^
clockType_Cayas.h:25:12: error: expected unqualified-id before ‘)’ token
  clockType();
            ^
clockType_Cayas.h:26:12: error: expected unqualified-id before ‘int’
  clockType(int hours, int minutes, int seconds);
            ^~~
clockType_Cayas.h:26:12: error: expected ‘)’ before ‘int’
  clockType(int hours, int minutes, int seconds);
           ~^~~
            )
clockType_Cayas.h:31:1: error: abstract declarator ‘<unnamed class>’ used as declaration
 };
 ^
clockType_Cayas.cpp:12:35: error: ‘void setHours(int)’ should have been declared inside ‘::’
 void clockType::setHours(int hours) {hr = hours;}
                                   ^
clockType_Cayas.cpp: In function ‘void setHours(int)’:
clockType_Cayas.cpp:12:38: error: ‘hr’ was not declared in this scope
 void clockType::setHours(int hours) {hr = hours;}
                                      ^~
clockType_Cayas.cpp: At global scope:
clockType_Cayas.cpp:13:39: error: ‘void setMinutes(int)’ should have been declared inside ‘::’
 void clockType::setMinutes(int minutes) {min = minutes;}
                                       ^
clockType_Cayas.cpp: In function ‘void setMinutes(int)’:
clockType_Cayas.cpp:13:48: error: overloaded function with no contextual type information
 void clockType::setMinutes(int minutes) {min = minutes;}
                                                ^~~~~~~
clockType_Cayas.cpp: At global scope:
clockType_Cayas.cpp:14:39: error: ‘void setSeconds(int)’ should have been declared inside ‘::’
 void clockType::setSeconds(int seconds) {sec = seconds;}
                                       ^
clockType_Cayas.cpp: In function ‘void setSeconds(int)’:
clockType_Cayas.cpp:14:42: error: ‘sec’ was not declared in this scope
 void clockType::setSeconds(int seconds) {sec = seconds;}
                                          ^~~
clockType_Cayas.cpp:14:42: note: suggested alternative: ‘getc’
 void clockType::setSeconds(int seconds) {sec = seconds;}
                                          ^~~
                                          getc
clockType_Cayas.cpp: At global scope:
clockType_Cayas.cpp:21:25: error: ‘int getHours()’ should have been declared inside ‘::’
 int clockType::getHours() {return hr;}
                         ^
clockType_Cayas.cpp: In function ‘int getHours()’:
clockType_Cayas.cpp:21:35: error: ‘hr’ was not declared in this scope
 int clockType::getHours() {return hr;}
                                   ^~
clockType_Cayas.cpp: At global scope:
clockType_Cayas.cpp:22:27: error: ‘int getMinutes()’ should have been declared inside ‘::’
 int clockType::getMinutes() {return min;}
                           ^
clockType_Cayas.cpp: In function ‘int getMinutes()’:
clockType_Cayas.cpp:22:37: error: cannot resolve overloaded function ‘min’ based on conversion to type ‘int’
 int clockType::getMinutes() {return min;}
                                     ^~~
clockType_Cayas.cpp: At global scope:
clockType_Cayas.cpp:23:27: error: ‘int getSeconds()’ should have been declared inside ‘::’
 int clockType::getSeconds() {return sec;}
                           ^
clockType_Cayas.cpp: In function ‘int getSeconds()’:
clockType_Cayas.cpp:23:37: error: ‘sec’ was not declared in this scope
 int clockType::getSeconds() {return sec;}
                                     ^~~
clockType_Cayas.cpp:23:37: note: suggested alternative: ‘getc’
 int clockType::getSeconds() {return sec;}
                                     ^~~
                                     getc
clockType_Cayas.cpp: At global scope:
clockType_Cayas.cpp:26:31: error: ‘int getElapsedTime()’ should have been declared inside ‘::’
 int clockType::getElapsedTime() {return hr*60*60 + min*60 + sec;}
                               ^
clockType_Cayas.cpp: In function ‘int getElapsedTime()’:
clockType_Cayas.cpp:26:41: error: ‘hr’ was not declared in this scope
 int clockType::getElapsedTime() {return hr*60*60 + min*60 + sec;}
                                         ^~
clockType_Cayas.cpp:26:55: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator*’
 int clockType::getElapsedTime() {return hr*60*60 + min*60 + sec;}
                                                    ~~~^~~
clockType_Cayas.cpp:26:61: error: ‘sec’ was not declared in this scope
 int clockType::getElapsedTime() {return hr*60*60 + min*60 + sec;}
                                                             ^~~
clockType_Cayas.cpp:26:61: note: suggested alternative: ‘getc’
 int clockType::getElapsedTime() {return hr*60*60 + min*60 + sec;}
                                                             ^~~
                                                             getc
clockType_Cayas.cpp: At global scope:
clockType_Cayas.cpp:29:33: error: ‘int getRemainingTime()’ should have been declared inside ‘::’
 int clockType::getRemainingTime() {return 24*60*60 - getElapsedTime();}
                                 ^
clockType_Cayas.cpp:33:29: error: variable or field ‘compareClock’ declared void
 void clockType::compareClock(clockType& otherClock) {
                             ^
clockType_Cayas.cpp:33:41: error: ‘otherClock’ was not declared in this scope
 void clockType::compareClock(clockType& otherClock) {
                                         ^~~~~~~~~~
clockType_Cayas.cpp:61:21: error: expected id-expression before ‘(’ token
 clockType::clockType() {
                     ^
clockType_Cayas.cpp:67:21: error: expected id-expression before ‘(’ token
 clockType::clockType(int hours, int minutes, int seconds) {

Что может быть причиной этих ошибок и как я могу это исправить?

...