visual 将类分为 cpp 和头文件 (C++)
visual Separating a class into cpp and header file (C++)
我是C++语言的新手。因此,我被分配将现有文件拆分为三个源代码:swap.h,swap.cpp和source3.cpp
现有文件:
#include <iostream>
void get_numbers (int&, int&);
void swap_values (int&, int&);
void show_results (int, int);
int main () {
int first_num, second_num;
get_numbers (first_num, second_num);
swap_values (first_num, second_num);
show_results (first_num, second_num);
return 0;
}
void get_numbers (int& input1, int& input2) {
using namespace std;
cout << "Enter two integers: ";
cin >> input1 >> input2;
}
void swap_values (int& variable1, int& variable2) {
int temp;
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
void show_results (int output1, int output2) {
using namespace std;
cout << "In reverse order the numbers are: "
<< output1 << " " << output2 << endl;
}
swap.h 包含函数原型
swap.cpp 包含函数实现
source3.cpp 包含主函数
对于交换.h:
#pragma once
#ifndef swap_h
#define swap_h
void get_numbers(int&, int&);
void swap_values(int&, int&);
void show_results(int, int);
#endif
用于交换.cpp
#include <iostream>
void get_numbers(int& input1, int& input2) {
using namespace std;
cout << "Enter two integers: ";
cin >> input1 >> input2;
}
void swap_values(int& variable1, int& variable2) {
int temp;
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
void show_results(int output1, int output2) {
using namespace std;
cout << "In reverse order the numbers are: "
<< output1 << " " << output2 << endl;
}
对于源 3.cpp:
#include "stdafx.h"
#include "swap.h"
int main()
{
int first_num, second_num;
get_numbers(first_num, second_num);
swap_values(first_num, second_num);
show_results(first_num, second_num);
return 0;
}
当我调试程序时,它说:"无法启动程序'C:\User...'系统找不到指定的文件。我做错了什么?
由于代码编译成功,但无法启动,因此可能存在与调试环境相关的问题。
此外,一旦您拥有#pragma once
,您就不需要#ifdef
、#define
和#endif
。
如果您提供的是整个代码,则您没有在swap.cpp
中包含swap.h
。因此,您有函数的定义,但没有声明。虽然我会想象另一个错误或至少在这里发出警告。尝试解决这个问题。
如果不起作用,请尝试构建发布版本。它编译吗?它开始了吗?当它启动时,它会做任何事情吗?如果我之前提到的是问题所在,我希望程序只是运行到最后,而不做任何事情。
如果问题出在主文件中的swap.h
上,请确保它位于同一位置,或者包含路径指向包含它的目录。stdafx.h
也是如此
此外,您不需要#pragma once
和#ifndef
#define
和#endif
。摆脱其中任何一个,我建议使用 #ifndef
#define
和 #endif
,因为并非到处都支持#pragma once
。但对你来说,这无关紧要。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到