如何将这个VC ++ 6代码转换为VC ++ 2008?

时间:2021-10-31 21:03:05

Forgive me my C++ is incredibly rusty. But I am trying to take some old code and recompile it under Visual C++ 2008. It was originally written for Visual C++ 6.0

原谅我,我的C ++非常生疏。但我正在尝试使用一些旧代码并在Visual C ++ 2008下重新编译它。它最初是为Visual C ++ 6.0编写的

The error I am getting is this:

我得到的错误是这样的:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int

Ok seems simple enough. But then I look at the offending line of code:

好吧看起来很简单。但后来我看看有问题的代码行:

operator=(int i) {SetAsInt(i);};

And it appears the type IS declared. So what am I missing?

它似乎声明了类型IS。那么我错过了什么?

FOLLOW UP:

I took Micheals advice and added a return type of the function (the class), and added return this; to the end of each. Then I ran across this:

我接受了Micheals的建议,并添加了函数的返回类型(类),并添加了返回;到每个人的结尾。然后我碰到了这个:

operator=(const CString& str);

There is no function body defined... what exactly does this mean?

没有定义函数体......究竟是什么意思?

7 个解决方案

#1


You need to have the operator=() method return something (it would assume int if the diagnostic weren't an error, as the error message somewhat confusingly indicates).

你需要让operator =()方法返回一些东西(如果诊断不是错误,它将假定为int,因为错误消息有点令人困惑地指示)。

Generally it would be a reference to the object the operator is working on so the assignments can be chained as in a normal assignment expression. Something like:

通常,它将是对操作员正在处理的对象的引用,因此可以将分配链接为正常的赋值表达式。就像是:

// where T is the class for this operator= implementation

T& operator=( int i) {
     // ...
    return *this;
}

#2


As michael has said, operator= needs a return type, but this type can be void:

正如迈克尔所说,operator =需要一个返回类型,但这种类型可能是无效的:

class A {
   ...  
   void operator = ( int i ) {
      SetAsInt(i);
   }
};

although this means you won't be able to "daisy-chain" assignments.

虽然这意味着你将无法进行“菊花链”任务。

Regarding the follow-up, it's probably saying that assignment for the class is forbidden:

关于后续行动,可能会说禁止为班级分配:

class B {
    private:
        void operator =( const B & );    
};

Making the assignment op (and usually the copy ctor) private and then not implementing them means it is impossible to assign (or copy) class instances, which is desirable behaviour for most business -oriented classes.

使赋值op(通常是copy ctor)私有然后不实现它们意味着不可能分配(或复制)类实例,这对于大多数面向业务的类来说是理想的行为。

#3


For your second question, the declaration is likey being used to prevent copying the object.

对于第二个问题,声明就像用于防止复制对象一样。

From the C++ Reference Guide by Danny Kalev

来自Danny Kalev的C ++参考指南

Question: How Can I Prevent Object-Copying?

问题:如何防止对象复制?

Answer: Declare the copy constructor and the assignment operator as private members, without defining them. Any statement that involves direct or indirect copying of that class will cause a compilation error. This technique isn’t exactly the picture of elegance, but in contemporary C++ there’s no other way to block copying inexpensively.

答案:将复制构造函数和赋值运算符声明为私有成员,而不定义它们。任何涉及直接或间接复制该类的语句都将导致编译错误。这种技术并不完全是优雅的图景,但在现代C ++中,没有其他方法可以低成本地阻止复制。

#4


Regarding your edit: It simply means that the function has been declared, but not defined. The compiler knows it exists, so it is legal to call it. But it will generate a linker error, unless the compiler is actually able to find the body somewhere.

关于你的编辑:它只是意味着该函数已被声明,但未定义。编译器知道它存在,因此调用它是合法的。但它会生成链接器错误,除非编译器实际上能够在某处找到主体。

Typically, you define the function in a header file, and then in the .cpp file, you have something like

通常,您在头文件中定义函数,然后在.cpp文件中,您有类似的东西

// Assuming the class it is a member of is called Foo
Foo& Foo::operator=(const CString& str) {
 ...
}

The only thing to note here is the Foo:: prefix. Because this definition is outside the class definition itself, we have to use this prefix to specify that the operator= we're defining belongs to the Foo class.

这里唯一需要注意的是Foo ::前缀。因为这个定义不在类定义本身之内,所以我们必须使用这个前缀来指定operator =我们定义属于Foo类。

#5


returning ANYTHING but an lval is typically wrong and is only used in very special situations

返回ANYTHING但是lval通常是错误的,仅用于非常特殊的情况

certainly returning a const reference precludes assignment chaining (a=(b=c)), and part of the point of operators is to make classes behave like built in types

肯定返回一个const引用排除了赋值链(a =(b = c)),而运算符的一部分就是使类的行为类似于内置类型

#6


Well, that's an assignment operator. It helps one define how other objects (both of the same and other types) are assigned to an instance of the class it is defined within.

好吧,那是一个赋值算子。它有助于定义如何将其他对象(包括相同类型和其他类型)分配给它所定义的类的实例。

The correct syntax is (assuming your class is called 'Object'):

正确的语法是(假设您的类被称为'对象'):

const Object& operator=(const Object& other)
{
  // copy members, one at a time.
  this->member1 = other.member1;
  return *this;
}

Notice the return type is constant. This is a to avoid semantically incorrect, syntactically correct statements such as:

请注意,返回类型是常量。这是为了避免语义错误,语法正确的语句,例如:

Object A, B, C;

(A = B) = C;

If you define the return type as constant, the above code will not compile (reasonable enough, since it's really messed up) while leaving out the const will allow such bad code to compile, and of course someone will pull their hair trying to figure out what's wrong.

如果你将返回类型定义为常量,上面的代码将无法编译(合理的,因为它真的搞砸了),而省略const将允许这样的错误代码编译,当然有人会拉他们的头发试图弄清楚怎么了。

P.S. You might want to think it through: what would happen if you leave out the const from the return type and execute the (A = B) = C; code?

附:您可能想要考虑一下:如果从返回类型中省略const并执行(A = B)= C,会发生什么?码?

#7


Regarding the follow-up question, the return type can be anything in this case. However, since most assignment operators return a reference to the class in which they are declared, it would be best to return just that, in order not to confuse the reader.

关于后续问题,在这种情况下,返回类型可以是任何内容。但是,由于大多数赋值运算符都返回对声明它们的类的引用,因此最好只返回它,以免混淆读者。

CString& operator=(const CString& str);

Presumably, this function is declared in the private section of the class. This is the standard idiom to make objects of the class non-assignable.

据推测,此函数在类的私有部分中声明。这是使类的对象不可赋值的标准习惯用法。

#1


You need to have the operator=() method return something (it would assume int if the diagnostic weren't an error, as the error message somewhat confusingly indicates).

你需要让operator =()方法返回一些东西(如果诊断不是错误,它将假定为int,因为错误消息有点令人困惑地指示)。

Generally it would be a reference to the object the operator is working on so the assignments can be chained as in a normal assignment expression. Something like:

通常,它将是对操作员正在处理的对象的引用,因此可以将分配链接为正常的赋值表达式。就像是:

// where T is the class for this operator= implementation

T& operator=( int i) {
     // ...
    return *this;
}

#2


As michael has said, operator= needs a return type, but this type can be void:

正如迈克尔所说,operator =需要一个返回类型,但这种类型可能是无效的:

class A {
   ...  
   void operator = ( int i ) {
      SetAsInt(i);
   }
};

although this means you won't be able to "daisy-chain" assignments.

虽然这意味着你将无法进行“菊花链”任务。

Regarding the follow-up, it's probably saying that assignment for the class is forbidden:

关于后续行动,可能会说禁止为班级分配:

class B {
    private:
        void operator =( const B & );    
};

Making the assignment op (and usually the copy ctor) private and then not implementing them means it is impossible to assign (or copy) class instances, which is desirable behaviour for most business -oriented classes.

使赋值op(通常是copy ctor)私有然后不实现它们意味着不可能分配(或复制)类实例,这对于大多数面向业务的类来说是理想的行为。

#3


For your second question, the declaration is likey being used to prevent copying the object.

对于第二个问题,声明就像用于防止复制对象一样。

From the C++ Reference Guide by Danny Kalev

来自Danny Kalev的C ++参考指南

Question: How Can I Prevent Object-Copying?

问题:如何防止对象复制?

Answer: Declare the copy constructor and the assignment operator as private members, without defining them. Any statement that involves direct or indirect copying of that class will cause a compilation error. This technique isn’t exactly the picture of elegance, but in contemporary C++ there’s no other way to block copying inexpensively.

答案:将复制构造函数和赋值运算符声明为私有成员,而不定义它们。任何涉及直接或间接复制该类的语句都将导致编译错误。这种技术并不完全是优雅的图景,但在现代C ++中,没有其他方法可以低成本地阻止复制。

#4


Regarding your edit: It simply means that the function has been declared, but not defined. The compiler knows it exists, so it is legal to call it. But it will generate a linker error, unless the compiler is actually able to find the body somewhere.

关于你的编辑:它只是意味着该函数已被声明,但未定义。编译器知道它存在,因此调用它是合法的。但它会生成链接器错误,除非编译器实际上能够在某处找到主体。

Typically, you define the function in a header file, and then in the .cpp file, you have something like

通常,您在头文件中定义函数,然后在.cpp文件中,您有类似的东西

// Assuming the class it is a member of is called Foo
Foo& Foo::operator=(const CString& str) {
 ...
}

The only thing to note here is the Foo:: prefix. Because this definition is outside the class definition itself, we have to use this prefix to specify that the operator= we're defining belongs to the Foo class.

这里唯一需要注意的是Foo ::前缀。因为这个定义不在类定义本身之内,所以我们必须使用这个前缀来指定operator =我们定义属于Foo类。

#5


returning ANYTHING but an lval is typically wrong and is only used in very special situations

返回ANYTHING但是lval通常是错误的,仅用于非常特殊的情况

certainly returning a const reference precludes assignment chaining (a=(b=c)), and part of the point of operators is to make classes behave like built in types

肯定返回一个const引用排除了赋值链(a =(b = c)),而运算符的一部分就是使类的行为类似于内置类型

#6


Well, that's an assignment operator. It helps one define how other objects (both of the same and other types) are assigned to an instance of the class it is defined within.

好吧,那是一个赋值算子。它有助于定义如何将其他对象(包括相同类型和其他类型)分配给它所定义的类的实例。

The correct syntax is (assuming your class is called 'Object'):

正确的语法是(假设您的类被称为'对象'):

const Object& operator=(const Object& other)
{
  // copy members, one at a time.
  this->member1 = other.member1;
  return *this;
}

Notice the return type is constant. This is a to avoid semantically incorrect, syntactically correct statements such as:

请注意,返回类型是常量。这是为了避免语义错误,语法正确的语句,例如:

Object A, B, C;

(A = B) = C;

If you define the return type as constant, the above code will not compile (reasonable enough, since it's really messed up) while leaving out the const will allow such bad code to compile, and of course someone will pull their hair trying to figure out what's wrong.

如果你将返回类型定义为常量,上面的代码将无法编译(合理的,因为它真的搞砸了),而省略const将允许这样的错误代码编译,当然有人会拉他们的头发试图弄清楚怎么了。

P.S. You might want to think it through: what would happen if you leave out the const from the return type and execute the (A = B) = C; code?

附:您可能想要考虑一下:如果从返回类型中省略const并执行(A = B)= C,会发生什么?码?

#7


Regarding the follow-up question, the return type can be anything in this case. However, since most assignment operators return a reference to the class in which they are declared, it would be best to return just that, in order not to confuse the reader.

关于后续问题,在这种情况下,返回类型可以是任何内容。但是,由于大多数赋值运算符都返回对声明它们的类的引用,因此最好只返回它,以免混淆读者。

CString& operator=(const CString& str);

Presumably, this function is declared in the private section of the class. This is the standard idiom to make objects of the class non-assignable.

据推测,此函数在类的私有部分中声明。这是使类的对象不可赋值的标准习惯用法。