简体   繁体   English

构造函数在定义时使用自身时出错

[英]Constructor using itself while being defined error

Error 1 error C2460: 'Square::string' : uses 'Square', which is being defined 错误1错误C2460:'Square :: string':使用正在定义的'Square'

Hello everyone, I am getting the following error while trying to implement my 4-square cipher. 大家好,我在尝试实施4平方密码时遇到以下错误。 I have two constructors in the Square header and .cpp file. 我在Square标头和.cpp文件中有两个构造函数。 One of them takes no parameters while the other is taking a string. 其中一个不带任何参数,而另一个带一个字符串。 The one with the string is throwing the error. 带有字符串的那个抛出错误。

square.h square.h

#include <string>

#ifndef SQUARE_H
#define SQUARE_H
class Square
{
public:
    Square();
    Square(string);
    ~Square();
    char mat[5][5];
    bool used[25];
    char getChar(int, int);
    int* getPos(char);

};

#endif

Here is the layout of it in the .cpp file. 这是.cpp文件中的布局。

Square::Square(string s)
{
//Code in here can show on request!
}

I think the string piece has something to do with it but I am not sure. 我认为弦乐与它有关,但我不确定。

Instead of 代替

Square( string );

you need 你需要

Square( std::string );

There may be other errors in the code not shown. 未显示的代码中可能还有其他错误。


Not an error, but usually a std::string is passed by reference, to avoid needless copying. 这不是错误,但通常通过引用传递std::string ,以避免不必要的复制。


Also, tip: with modern compilers, instead of include guards you can just use #pragma once . 另外,提示:对于现代编译器,您可以仅使用#pragma once ,而不必使用包含保护程序。 Keeping in mind that it's a universally supported de facto standard, not part of the official standard. 请记住,这是一个普遍支持的事实标准,而不是正式标准的一部分。 I find it cleaner, somehow, and it alleviates the need to come up with distinct, unique include guard symbols. 我发现它以某种方式更干净,并且减轻了提出独特,独特的包含保护符号的需要。

There's not enough to really know what's going on here. 还不足以真正知道这里发生了什么。 Everything looks fine from what you have. 您所拥有的一切看起来都很好。

Maybe you need the namespace? 也许您需要命名空间?

Square(std::string);

See (here on so: Error : Class A uses Class B, which is being defined ) and also here . 参见(此处: 错误:类A使用正在定义的类B ),也请参见此处

string is the problem. 字符串是问题。 It's from std namespace so prefix every occurrence of string with 'std::', eg for your header: 它来自std名称空间,因此,在字符串的每个出现位置都以'std ::'作为前缀,例如,用于标头:

#include <string>

#ifndef SQUARE_H
#define SQUARE_H
class Square
{
public:
    Square();
    Square(std::string);
    ~Square();
    char mat[5][5];
    bool used[25];
    char getChar(int, int);
    int* getPos(char);

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM