Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用头文件从.cpp创建对象文件_C++_Compiler Errors_Linker_Object Code - Fatal编程技术网

C++ 如何使用头文件从.cpp创建对象文件

C++ 如何使用头文件从.cpp创建对象文件,c++,compiler-errors,linker,object-code,C++,Compiler Errors,Linker,Object Code,我试图通过在linux终端中键入以下内容,使用g++创建一个对象文件: user$g++-c clockType_Cayas.cpp clockType_Cayas.h /*文件:clockType_Cayas.h *作者:冯·维克·卡亚斯 *日期:2019年10月27日 */ #ifndef时钟型 #定义时钟类型 类钟型{ 公众: //设置小时、分钟或秒 无效设置小时数(整数小时); 无效设置分钟数(整数分钟); 无效设置秒(整数秒); //获得小时、分钟或秒 int getHours();

我试图通过在linux终端中键入以下内容,使用g++创建一个对象文件:

user$g++-c clockType_Cayas.cpp
clockType_Cayas.h

/*文件:clockType_Cayas.h
*作者:冯·维克·卡亚斯
*日期:2019年10月27日
*/
#ifndef时钟型
#定义时钟类型
类钟型{
公众:
//设置小时、分钟或秒
无效设置小时数(整数小时);
无效设置分钟数(整数分钟);
无效设置秒(整数秒);
//获得小时、分钟或秒
int getHours();
int getMinutes();
int getSeconds();
//获取经过的时间和剩余时间
int getElapsedTime();
int getRemainingTime();
无效比较锁(时钟类型和其他时钟);
时钟类型();
时钟类型(整数小时、整数分钟、整数秒);
私人:
国际人力资源;
int-min;
国际秒;
};
#恩迪夫
clockType_Cayas.cpp

/*文件:clockType_Cayas.cpp
*作者:冯·维克·卡亚斯
*日期:2019年10月27日
*/
#包括
#包括
#包括“clockType_Cayas.h”
使用名称空间std;
//二传手
// #####################################################
//对于clockType,设置hr、min和sec专用变量
void clockType::setHours(int hours){hr=hours;}
void clockType::setMinutes(int minutes){min=minutes;}
void clockType::setSeconds(int seconds){sec=seconds;}
// #####################################################
//吸气剂
// #####################################################
//对于clockType,返回hr、min和sec私有变量
int clockType::getHours(){return hr;}
int clockType::getMinutes(){return min;}
int clockType::getSeconds(){return sec;}
//对于clockType,返回以秒为单位的总运行时间
int clockType::getElapsedTime(){返回hr*60*60+分钟*60+秒;}
//对于clockType,返回总剩余时间(以秒为单位)
int clockType::getRemainingTime(){return 24*60*60-getElapsedTime();}
// #####################################################
//比较clockType的两个对象,比较它们的总运行时间
void clockType::compareClock(clockType和otherClock){
浮点数(浮点数a);
float diff=abs(otherClock.getElapsedTime()-getElapsedTime());//以秒为单位获取与时钟的差值
//从十进制秒到时间的转换
浮动小时、分钟、秒;
小时=diff/(60*60);
分钟=剩余时间(小时)*60;
秒=剩余时间(分钟)*60;
小时数=静态浇铸(小时);
分钟=静态施法(分钟);
秒=静态施法(秒);

不能使用与您的类相同的名称来定义clockType。这会导致issuesOh.Wow。所有这些错误都会随着您的解决方案神奇地消失…谢谢:)原因740预处理器是魔鬼。标头保护必须是完全唯一的。只有在非常罕见的情况下,您才应该使用保护elsewhere。非常罕见,以至于我真的想不出我推荐使用的任何一个。@user4581301我使用了一个联机GUID生成器,在尝试用重复的标题保护(它原来是两个库项目,每个项目都有一个utilities.h,显然是同一个保护)对一个大型(ish)项目(1000多个文件)进行排序之后。
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) {