您能帮助我理解为什么在这两个字符串中我会出现错误: 1) C2143:语法错误:缺失';‘前面’2)错误C4430:缺少类型说明符- int假设。注意: C++不支持默认-int。
MyString* m_pStr; // Link to a dynamically created string.
MyString* pPrev; // Pointer to the next counter.MyString.h
#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
#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
#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();
};发布于 2013-04-14 07:24:29
为了供其他人参考,我通常会发现导致此错误的原因如下:
发布于 2013-04-14 07:00:40
包含文件中有一个循环。编译器不会执行无限递归,因为您已经添加了#pragma once选项。
下面是编译器所做的工作:
#include "myString.h"。"myString.h"文件,找到#include "counter.h"。"counter.h"文件,找到#include "myString.h",但是因为#pragma once而忽略它。"counter.h",阅读MyString* m_pStr;行,不知道MyString是什么,失败的消息不太有用。现在,解决方案是在头文件中添加彼此类的声明。也就是说,将下面的行添加到myString.h的开头,就在includes之后。
class Counter;和下面的行到counter.h的开头
class MyString;现在,对于范围中的声明,但是没有类定义,您可以做一些事情,也可以做一些不能做的事情:基本上,您只能声明指针和引用。类的任何其他用途都必须转到CPP文件。
甚至可以去掉两个递归的includes!
https://stackoverflow.com/questions/15996426
复制相似问题