gpt4 book ai didi

c++ - "undeclared identifier"实际声明

转载 作者:可可西里 更新时间:2023-11-01 16:29:56 25 4
gpt4 key购买 nike

对于我在类头文件中声明为公共(public)数据成员的变量,一个 int 和一个指向该 int 的指针,我一直收到错误 C2065s。仅当我在函数中使用这些变量时,才会将代码行标记为错误 - 在类的构造函数中,它们似乎可以顺利通过。

我正在使用 Visual Studio 2010 Express 编写普通的 C++(不是 Visual C++),这里是编译器错误日志的输出:

1>------ Build started: Project: Project 2, Configuration: Debug Win32 ------
1> BaseClassWithPointer.cpp
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(27): error C2065: 'q' : undeclared identifier
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(27): error C2541: 'delete' : cannot delete objects that are not pointers
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(32): error C2065: 'num' : undeclared identifier
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(33): error C2065: 'q' : undeclared identifier
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(34): error C2065: 'q' : undeclared identifier
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最后,这是我的代码块和 header :

BaseClassWithPointer.h

#pragma once
#include <iostream>

using namespace std;

class BaseClassWithPointer
{
public:
int num;
int *q;
BaseClassWithPointer();
BaseClassWithPointer(int value);
BaseClassWithPointer(const BaseClassWithPointer& otherObject);
void destroyPointer();
virtual void print();
virtual ~BaseClassWithPointer(); //Destructor is virtual so that derived classes use their own version of the destructor. ~ (2. Inheritance - base class with pointer variables – destructors.)
const BaseClassWithPointer& operator= (const BaseClassWithPointer &rhs); //Assignment operator is overloaded to avoid shallow copies of pointers. ~ (3. Inheritance – base class with pointer variables – assignment operator overloading.)

};

BaseClassWithPointer.cpp

#pragma once
#include "BaseClassWithPointer.h"
#include <iostream>

using namespace std;

BaseClassWithPointer::BaseClassWithPointer()
{
num = 0;
q = &num;
}

BaseClassWithPointer::BaseClassWithPointer(int value)
{
num = value;
q = &num;
}

BaseClassWithPointer::BaseClassWithPointer(const BaseClassWithPointer& otherObject)
{
num = otherObject.num;
q = &num;
}

void destroyPointer()
{
delete q;
}

void print()
{
cout << "Num: " << num << endl;
cout << "Value pointed to by q: " << *q << endl;
cout << "Address of q: " << q << endl;
}

BaseClassWithPointer::~BaseClassWithPointer()
{
destroyPointer();
}

const BaseClassWithPointer& BaseClassWithPointer::operator=(const BaseClassWithPointer &rhs)
{
if (this != &rhs)
{
num = rhs.num;
q = &num;
}

return *this;
}

最佳答案

您忘记了 destroyPointer() 方法的类标识符。尝试

void BaseClassWithPointer::destroyPointer()

代替

关于c++ - "undeclared identifier"实际声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12609284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com