首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的类不可访问,而我包含了头文件

为什么我的类不可访问,而我包含了头文件
EN

Stack Overflow用户
提问于 2013-04-14 06:32:59
回答 2查看 11.8K关注 0票数 0

您能帮助我理解为什么在这两个字符串中我会出现错误: 1) C2143:语法错误:缺失';‘前面’2)错误C4430:缺少类型说明符- int假设。注意: C++不支持默认-int。

代码语言:javascript
复制
MyString* m_pStr; // Link to a dynamically created string.
MyString* pPrev; // Pointer to the next counter.

MyString.h

代码语言:javascript
复制
#pragma once
#include <iostream>
#include "counter.h"

using namespace std;
class MyString
{
    char* m_pStr;   //String which is a member of the class.
    void CreateArray(const char * pStr);
    Counter* m_pMyCounter; // Pointer to its own counter. 

    public:
        MyString(const char* pStr = "");
        MyString(const MyString & other);
        MyString(MyString && other);
        ~MyString();

        const char * GetString();
        void SetNewString(char * str);

        void printAllStrings(); 
        void ChangeCase();
        void printAlphabetically();
};

MyString.cpp

代码语言:javascript
复制
#include "myString.h"
#include <iostream>
using namespace std;


MyString::MyString(const char* pStr){   
    this->CreateArray(pStr);
    strcpy(m_pStr, pStr);   
};
void MyString:: CreateArray(const char * pStr){
    int size_of_string = strlen(pStr)+1;    
    m_pStr = new char[size_of_string];  
}

MyString::MyString(const MyString & other){
    this->CreateArray(other.m_pStr);
    strcpy(m_pStr, other.m_pStr);
}

MyString::MyString(MyString && other){
    this->m_pStr = other.m_pStr;
    other.m_pStr = nullptr; 
}

MyString::~MyString(){
    delete[] m_pStr;
}

const char * MyString:: GetString(){
    return m_pStr;
}

void MyString:: SetNewString(char * str){
    this->CreateArray(str);
    strcpy(m_pStr, str);
}

comp.h

代码语言:javascript
复制
#pragma once
#include "myString.h"
#include <iostream>
using namespace std;

class Counter{
    private:
        MyString* m_pStr; // Link to a dynamically created string.
        int m_nOwners; // Counter of users of this string.
        MyString* pPrev; // Pointer to the next counter.
    public:
        Counter(); 
        //Copy constructor.
        ~Counter();
        void AddUser();
        void RemoveUser();
};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-14 07:24:29

为了供其他人参考,我通常会发现导致此错误的原因如下:

  • 循环包含(是的,这次就是你)。头A依赖于B依赖于A,这意味着当您首先包含A时,B包含在它上面。B试图将A放在它上面(再次),但是“语用一次”或包含保护阻止了这一点。结果是B在它上面没有A的定义,但是如果没有=>这个错误就无法工作。
  • 包涵体防护器。您创建了新的头文件,许多人习惯于复制/粘贴现有的头文件。这个新的头文件可能会忘记调整它的包含保护程序。如果发生这种情况,就会有一个include,但是包含保护程序只会让新的或旧的头文件进入,不管是第一个。最终的结果是,在一些包含您的新头的文件中,它将无法工作,类也将未定义。也许会持续很长一段时间,我已经看到代码库中的这种情况存在多年而未被检测到。不符合“实用主义一次”用户的资格。
  • 实际上忘记写类/排字在使用或定义/大写不同。明显的问题,明显的解决办法。
票数 2
EN

Stack Overflow用户

发布于 2013-04-14 07:00:40

包含文件中有一个循环。编译器不会执行无限递归,因为您已经添加了#pragma once选项。

下面是编译器所做的工作:

  1. 读取CPP文件。找到#include "myString.h"
  2. 读取"myString.h"文件,找到#include "counter.h"
  3. 读取"counter.h"文件,找到#include "myString.h",但是因为#pragma once而忽略它。
  4. 继续使用"counter.h",阅读MyString* m_pStr;行,不知道MyString是什么,失败的消息不太有用。

现在,解决方案是在头文件中添加彼此类的声明。也就是说,将下面的行添加到myString.h的开头,就在includes之后。

代码语言:javascript
复制
class Counter;

和下面的行到counter.h的开头

代码语言:javascript
复制
class MyString;

现在,对于范围中的声明,但是没有类定义,您可以做一些事情,也可以做一些不能做的事情:基本上,您只能声明指针和引用。类的任何其他用途都必须转到CPP文件。

甚至可以去掉两个递归的includes

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15996426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档