简体   繁体   English

如何在标头和 .cpp 文件中正确声明返回自己定义类型的函数?

[英]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.

相关问题 如何在.cpp 文件中 function 的返回类型(签名)中使用在 header 文件中定义的别名? - How can I use an alias thats defined in a header file, in the return type (signature) of a function in the .cpp file? 如何在头文件中声明类型转换并在cpp文件中实现? - how to declare type conversion in header file and implement in cpp file? 如何在cpp文件的头文件中声明一个类函数? - How to declare a class function in header file in cpp file? 我们如何将带有类模板的友元函数声明到 .h 文件中并将它们定义到 .cpp 文件中(不是全部在一个头文件中)? - How do we declare a friend function with a class template into .h file and define them into a .cpp file (not all in one header file)? 如何在 Cython 中定义返回 cpp 定义类型的 function? - How to define a function returning a cpp-defined type in Cython? 如何声明具有在声明下方定义的类型的参数的 function? - How do I declare a function that has a parameter of a type that's defined below the declaration? 如何声明头文件中的变量在两个.cpp中使用? - How to declare a variable in header file to be used in two .cpp? Function 在header中定义并从cpp文件中调用出错 - Function defined in the header and called from the cpp file error 我需要在make文件中声明头文件或cxx文件还是两者都声明? - Do I need to declare header files or cxx files or both in a make file? 如何在头文件中声明常量对 - How do I declare a constant pair inside my header file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM