C ++包含文件会导致未识别的错误

时间:2018-02-27 15:16:44

标签: c++ header

我有两个导致问题的课程 - Class" A"和班级" B"。 A类必须能够存储B类对象,但B类中的函数必须接受A类参数。我的代码如下。

A类:

#ifndef INC_A
#define INC_A
#include "ClassB.h"

class ClassA
{
private:
    ClassB* b;
public:
    void Foo()
    {
        b = new ClassB;
        b->Foo2(this);
    }
}

#endif

B组:

#ifndef INC_B
#define INC_B
#include "ClassA.h"

class ClassB
{
public:
    void Foo2(ClassA* a)
    {
        // Do stuff with "a" here
    }
}

#endif

我得到的错误如下:

'ClassB::Foo2': function does not take 1 arguments

syntax error: identifier 'ClassA'

有没有办法解决这个看似无限的包含循环?

3 个答案:

答案 0 :(得分:2)

最简单的情况:

import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
Button,
Image,
} from 'react-native';


let timeoutid;

export default class Splash extends Component {

static navigationOptions = {
navbarHidden: true,
tabBarHidden: true,
};

constructor(props) {
super(props)
this.state = { navigatenow: false };

 }
 componentDidMount() {
 timeoutid=setTimeout(() => {
   this.setState({ navigatenow: true });
 }, 5000);
}
 componentWillUnmount(){

 clearTimeout(timeoutid);
}
componentDidUpdate(){
const { navigate,goBack } = this.props.navigation;

if (this.state.navigatenow == true) {

  navigate('Main');
}
}
render() {

//instead of writing this code in render write this code in 
 componenetDidUdpate method 
/* const { navigate,goBack } = this.props.navigation;

if (this.state.navigatenow == true) {

  navigate('Main');
}*/

return (

  <Image style={{
    flex: 1, width: null,
    height: null,
    resizeMode: 'cover'
  }} source={require('./login.png')}>
  </Image>
);

  }
}

在更复杂的场景中,可能需要提取抽象接口,然后将这两个类作为后代。

答案 1 :(得分:-1)

是的,您需要将您的类分成标题和实现文件(.h和.cpp),然后使用前向定义。例如:

ClassA.h:

#include "ClassA.h"

void ClassA::Foo() {
        b = new ClassB;
        b->Foo2(this);
}

ClassA.cpp:

/* Custom Hero Styles */
    
    .subhero1 {
        background: #000;
        height: 400px !important;
    }
    
    @media only screen and (min-width: 0px) and (max-width: 650px) {
        .subhero1 {
            background: #000;
            height: 250px !important;
        }
        .subhero__content-container {
            color: #fff !important;
        }
    }
    /* Custom Cards */
    
    .obs__fullcardheader {
        border-bottom: 0px;
        margin-bottom: 0px;
        padding-bottom: 0px;
        font-size: 3.5vmax;
    }
    
    .obs__fullcard {
        height: 400px;
        width: 100%;
        border: 5px solid #eee;
    }
    
    .obs__cardheader img {
        max-width: 20%;
    }
    
    .obs__card {
        height: 300px;
    }
    
    .fullcardwrapper {
        background: #f6f6f6;
        background-size: cover;
        background-position: center;
        padding-top: 30px;
        line-height: 30px;
    }
    
    .cardwrapper {
        padding: 30px;
    }
    
    .appicons img {
        max-width: 160px;
    }
    
    .small__font {
        font-size: 12px;
    }
    
    .obs__cardline2 {
        font-size: 14px;
    }
    
    .obs__cardline1 a {
        color: #000;
        text-decoration: none;
        cursor: pointer;
    }
    
    .obs__cardline1 a:hover {
        color: #565656;
        border-bottom: 1px solid #565656;
    }
    
    .obs__cardline2 a {
        color: #000;
        text-decoration: none;
        cursor: pointer;
    }
    
    .obs__cardline2 a:hover {
        color: #565656;
        border-bottom: 1px solid #565656;
    }
    
    .coupon-btn-wrapper {
        margin: 0 auto;
        max-width: 300px;
    }
    
    .print-exclusions {
        display: none;
    }
    
    @media (max-width: 650px) {
        .obs__card {
            width: 100%;
        }
        .obs__cardheader img {
            max-width: 15% !important;
        }
        .obs__fullcardheader {
            font-size: 4.5vmax;
        }
        .obs__couponhero-line1 {
            padding-top: 20px;
        }
    }
    
    @media (max-width: 1050px) {
        .obs__card {
            width: 100%;
            height: 250px;
        }
        .obs__cardheader img {
            max-width: 10%;
        }
        .cardwrapper {
            padding: 0px;
            margin-top: 0px;
        }
        .fullcardwrapper {
            padding-top: 10px;
            background: #fff;
        }
        .obs__fullcard {
            border: 1px solid #eee;
        }
        .obs__fullcardheader {
            font-size: 4.5vmax;
        }
        .coupon-btn-wrapper {
            max-width: 280px;
        }
    }
    
    .obs__couponhero-line1 {
        font-size: 14px;
        color: #000;
        margin-bottom: 10px;
        line-height: 18px;
        padding-top: 30px;
    }
    
    .obs__couponhero-line2 {
        font-size: 14px;
        color: #000;
        margin-bottom: 10px;
    }
    
    .obs__couponhero-barcode {
        margin: 0 auto;
    }
    
    .obs__couponhero-line3 {
        font-size: 14px;
        color: #000;
        margin-bottom: 10px;
        margin-top: -30px;
    }
    
    .couponlinks a {
        color: #fff;
        background: #000;
        text-decoration: none;
        font-size: 14px;
        padding: 15px;
    }
    
    @media print {
        @page {
            size: landscape
        }
        .obs__card {
            width: 33%;
            height: 250px;
        }
        .head-main,
        .obs__cardline2,
        .appicons,
        .cust_service_headline,
        .obs__accordion,
        .coupon-btn-wrapper {
            display: none;
        }
        .obs__fullcard {
            height: 260px;
        }
        .cardwrapper {
            margin-top: 10px;
            max-width: 97.8%;
        }
        .obs__cardheader img {
            max-width: 25%;
        }
        footer {
            display: none;
        }
        .print-exclusions {
            display: block;
            padding-left: 14px;
            margin-top: -115px;
            margin-bottom: 0;
            page-break-after: avoid;
            page-break-inside: avoid;
        }
        .obs__fullcard {
            width: 110%;
        }
        .obs-3 {
            clear: none;
        }
        @page {
            margin: 0;
        }
        body {
            margin: 1.6cm;
        }
    }

答案 2 :(得分:-2)

尝试在类定义的末尾添加分号。例如:

assert no_circular_dependencies(e, adisk)