在C++中,之间有什么区别:
struct Foo { ... };
Run Code Online (Sandbox Code Playgroud)
和
typedef struct { ... } Foo;
Run Code Online (Sandbox Code Playgroud) 在:http://www.learncpp.com/cpp-tutorial/19-header-files/
提到以下内容:
add.cpp:
int add(int x, int y)
{
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <iostream>
int add(int x, int y); // forward declaration using function prototype
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我们使用了前向声明,以便编译器在编译时知道"
add"是什么main.cpp.如前所述,为要在其他文件中使用的每个函数编写前向声明可能会很快变得乏味.
你能进一步解释" 前瞻性宣言 "吗?如果我们在main()函数中使用它会有什么问题?
可能重复:
c ++中的前向声明是什么?
我只是对这段代码在这个简单的例子中做了什么有疑问.我查找了朋友类并了解它们是如何工作的,但我不明白顶层的类声明实际上在做什么(即机器人).这是否意味着Happy类可以使用Robot对象,但是它们无法访问其私有部分,任何信息都将被欣赏.
#include <stdlib.h>
#include <stdexcept>
template <typename T> // What is this called when included
class Robot; // is there a special name for defining a class in this way
template <typename T>
class Happy
{
friend class Joe<T>;
friend class Robot<Happy<T> >;
// ...
};
Run Code Online (Sandbox Code Playgroud) 我有两个不同的结构,我想像这样彼此转换:
PointI a = PointI(3,5);
PointF b = a;
Run Code Online (Sandbox Code Playgroud)
我假设我需要做类似下面的代码:
struct PointF
{
PointF operator=(PointI point){
x = point.x;
y = point.y;
return *this;
}
float x, y;
};
struct PointI
{
PointI operator=(PointF point)
{
x = point.x;
y = point.y;
return *this;
}
int x, y;
};
Run Code Online (Sandbox Code Playgroud)
但问题是在宣布之前PointF使用PointI.从我在其他问题中读到的内容,我理解我可以PointI在定义两个结构之前声明,然后使用指针.虽然我似乎无法访问变量x和y指针,因为这些尚未定义.
有没有办法在定义它们之前将这些变量添加到struct声明中?或者有更好的方法来解决这个问题吗?
我遇到了以下我无法解决的错误.
1>k:\school\c++\project_2\project_2\mechanic.h(8): fatal error C1014: too many include files : depth = 1024
1> Maintenance.cpp
1>k:\school\c++\project_2\project_2\maintenance.h(8): fatal error C1014: too many include files : depth = 1024
(continues for many other files in the project)...
Run Code Online (Sandbox Code Playgroud)
项目的示例头文件:bicycle.h
//#pragma once //Make sure it's included only once
using namespace std;
#include <iostream>
#include "Date.h"
#include "Cyclist.h"
#include "Maintenance.h"
#ifndef BICYCLE_H_
#define BICYCLE_H_
class Bicycle
{
public:
Bicycle(Date, int, int, Cyclist);
~Bicycle(void);
Datum getDateJoined() const;
int getFrameSize() const;
int getBicycleID() const;
Cyclist getCyclist();
void …Run Code Online (Sandbox Code Playgroud) c++ ×5
declaration ×2
struct ×2
c ×1
c++-faq ×1
class ×1
definition ×1
header-files ×1
include ×1
terminology ×1
typedef ×1