相关疑难解决方法(0)

822
推荐指数
10
解决办法
35万
查看次数

801
推荐指数
5
解决办法
49万
查看次数

什么是C++中的前向声明?

在: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++ declaration forward-declaration

202
推荐指数
4
解决办法
25万
查看次数

头文件中的空类声明?

可能重复:
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)

c++ class header-files

4
推荐指数
1
解决办法
4552
查看次数

有两个结构在C++中引用彼此的变量

我有两个不同的结构,我想像这样彼此转换:

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在定义两个结构之前声明,然后使用指针.虽然我似乎无法访问变量xy指针,因为这些尚未定义.

有没有办法在定义它们之前将这些变量添加到struct声明中?或者有更好的方法来解决这个问题吗?

c++ struct

2
推荐指数
1
解决办法
1447
查看次数

"包含太多文件"错误C1014

我遇到了以下我无法解决的错误.

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++ include visual-studio

0
推荐指数
1
解决办法
4211
查看次数