[英]How do I properly declare a function returning own-defined type, both in the header and the .cpp file?
I am new to working with headers and the whole abstractization thing in C++ and I have noticed that: when you declare a function in the .cpp file, you first specify the return-type of the function, followed by namespace_name :: function_signature
, the name and the arguments of the function being the same you have already specified in the .h file.我刚开始使用 C++ 中的头文件和整个抽象事物,我注意到:当你在 .cpp 文件中声明一个函数时,你首先指定函数的返回类型,然后是
namespace_name :: function_signature
,函数的名称和参数与您已在 .h 文件中指定的相同。 Yet, how should I write the signature of a function whose forward-declaration ( if that is how the signatures in the .h file are called ) is present in the same namespace as the own-defined return type?然而,我应该如何编写一个函数的签名,该函数的前向声明(如果这是 .h 文件中的签名的调用方式)与自己定义的返回类型存在于相同的命名空间中? ( sorry for the verbose, code is simpler than my question; see below for clarification )
(抱歉冗长,代码比我的问题更简单;请参阅下文以进行澄清)
I want to find the longest sequence of equal numbers in a given array.我想在给定数组中找到最长的相等数字序列。 These are my files:
这些是我的文件:
//"sequences.h" file
#ifndef LAB1_SEQUENCES_H
#define LAB1_SEQUENCES_H
namespace Sequences{
/**
* Abstract Data Type for a sequence
*/
struct seq{
int i;
int j;
};
/**
* Finds longest sequence of equal integers
* arr - array to search in
* n - number of elements in the array
*/
seq longest_equal(int* arr, int n);
}
#endif //LAB1_SEQUENCES_H
. .
//"sequences.cpp" file
#include "sequences.h"
//Sequences :: seq Sequences :: longest_equal(int *arr, int n)
//seq Sequences :: longest_equal(int *arr, int n)
//struct seq longest_equal(int *arr, int n)
//Sequences :: struct seq longest_equal(int *arr, int n)
{
int i = 0, j = i + 1;
int lmax = -1, iR = -1, jR =-1 ; // lmax ar trebui sa fie 1 sau -1 ?
while (j < n )
{
if (arr[i] == arr[j]) j ++;
else
{
if (j - i > lmax) { lmax = j - i; iR = i; jR = j; }
i = j;
j = i + 1;
}
}
if (lmax > 1) return Sequences :: seq(iR, jR);
return Sequences::seq(-1, -1);
}
Now, I do not know how I can make my longest_equal
function in the .cpp file return seq
type (I am regarding both the signature in the header file and the signature in the .cpp file, as well as what I should virtually write before the return commands at the end of the function's implementation, in the .cpp file).现在,我不知道如何使 .cpp 文件中的
longest_equal
函数返回seq
类型(我关注头文件中的签名和 .cpp 文件中的签名,以及我之前应该虚拟写的内容.cpp 文件中函数实现结束时的返回命令)。
I have tried the commented lines of code just before the first accolade in the second file as signatures of the function.我在第二个文件中的第一个赞誉之前尝试了注释的代码行作为函数的签名。 I have also read the answers to the question here and some answers of the question here and tried something myself, yet to no avail.
我也看到了问题的答案在这里,问题的一些答案在这里,并试图自己的东西,但都无济于事。
Forward declarations are used to reduce header dependencies.前向声明用于减少标头依赖性。 Since your
struct seq
doesn't require any headers and all it has is data members, it makes little sense to forward declare it.由于您的
struct seq
不需要任何标头并且它只有数据成员,因此转发声明它毫无意义。 You should define it in the header along with the function declaration (and have the function definition in the .cpp
).您应该在头文件中定义它以及函数声明(并在
.cpp
有函数定义)。
You can use a forward declaration only for taking and returning structures and classes by reference or pointer.您只能将前向声明用于通过引用或指针获取和返回结构和类。 In your particular case the
struct seq
is a simple aggregate value, a tuple with named fields, conceptually.在您的特定情况下,
struct seq
是一个简单的聚合值,概念上是一个具有命名字段的元组。 It is not abstract data type as you state because there is nothing abstract about it, it is rather concrete and have no behaviour.它不是您所说的抽象数据类型,因为它没有任何抽象的东西,它相当具体并且没有行为。 An abstract data type is defined in terms of an behaviour (functions and member functions), rather than data members it is composed of.
抽象数据类型是根据行为(函数和成员函数)而不是组成它的数据成员来定义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.