我正在尝试创建一个简单的程序,使用良好的编程实践从C++中获取用户的输入.它由Input.hpp,Input.cpp和main.cpp组成.即使我使用ifndef来防止这种情况,我仍然会收到多重定义错误.
Input.hpp
#ifndef Input_HPP
#define Input_HPP
#include <string>
#include <vector>
using namespace std;
vector<string> Get_Input();
vector<string> input_array;
string starting_position;
int input_number;
#endif
Run Code Online (Sandbox Code Playgroud)
Input.cpp
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include "Input.hpp"
using namespace std;
vector<string> Get_Input()
{
cin>>starting_position;
cin>>input_number;
for (int i = 0; i < input_number; i++)
{
cin>>input_array[i];
}
cout<<"Done";
return input_array;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "Input.hpp"
#include <iostream>
using namespace std;
int main()
{
Get_Input();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我从头文件中删除变量声明并将它们放在cpp文件中但保留头文件中的函数声明时,程序构建没有错误.我的理解是变量和函数可以在头文件中声明.有人可以向我解释我错过了什么吗?
谢谢.
我是Prolog的新手,最近开始学习它,使用了很棒的书,现在学习Prolog!.有一些我不完全理解的东西,这真的让我烦恼.其中一个练习题是
我们有以下知识库和谓词:
child(anne,bridget).
child(bridget,caroline).
child(caroline,donna).
child(donna,emily).
descend(X,Y) :- child(X,Y).
descend(X,Y) :- child(X,Z),
descend(Z,Y).
Run Code Online (Sandbox Code Playgroud)
如果我们将谓词更改为以下内容会发生什么:
descend(X,Y) :- child(X,Y).
descend(X,Y) :- descend(X,Z),
descend(Z,Y).
Run Code Online (Sandbox Code Playgroud)
我知道这会导致对错误情况的无限递归,但我无法完全理解为什么.
如果我理解正确,在上面的第一种情况下,如果给出错误的查询child(X,Z)会耗尽其所有选项,试图将多个元素统一到Z然后失败,回溯到之前的X然后尝试再次选择Z以满足子项(X,Z) ).(如果我错了,请纠正我).
我不确定为什么下降谓词的第二个定义不会发生同样的情况.