C++ - "(常量字符*)"未在范围内声明?

C++ - '(const char*)' not declared in scope?

本文关键字:范围内 声明 常量 字符 C++      更新时间:2023-10-16

我想为C++制作一个自定义字符串类。但是当我这样做时:

g++ test.cpp sys/Base.h sys/Base.cpp

我收到此错误:

sys/Base.cpp: In function 'const char* Base::toChar()':
sys/Base.cpp:57:13: error: 'strval' was not declared in this scope
return strval;
^
sys/Base.cpp: In function 'std::string Base::toStr()':
sys/Base.cpp:60:20: error: 'strval' was not declared in this scope
return string(strval);
^

测试.cpp

#include "sys/Base.h"
int main() {
Base::write("Hello there.n");
return 0;
}

sys/Base.h

// Header file handling
#ifndef ARAVK_BASE_H
#define ARAVK_BASE_H
// Includes
#include <string>
// Global variables
#define EXIT_YAY 0
#define EXIT_ERR 1
using namespace std;
namespace Base {
// Classes:
class String {
static const char* strval;
public:
// Constructors:
String();
String(char[]);
String(const char*);
String(string);
// Destructors:
~String();
// Operators:
// =
void operator=(const String&);
void operator=(const char*&);
void operator=(const string&);
// Conversion:
const char* toChar() const;
string toStr() const;
};
// Functions:
// Input-Output:
// Write:
void write(String);
void write(string);
void write(const char*);
// Read:
String read();
// Executing:
String run(String);
}
#endif

系统/基础.cpp

// Including
#include "Base.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
// Global variables
#define EXIT_ERR 1
#define EXIT_YAY 0
/* ------------------------ */
using namespace std;
namespace Base {
// Classes
// String functions
// Constructors
String::String() {
const char* strval = "";
}
String::String(const char* str) {
const char* strval = str;
}
String::String(string str) {
const char* strval = str.c_str();
}
String::String(char str[]) {
const char* strval = str;
}
// Destructors
String::~String() {
delete strval;
}
// Operators
// =
void String::operator=(const String &strp) {
strval = strp.toChar();
}
void String::operator=(const char* &strp) {
strval = strp;
}
void String::operator=(const string &strp) {
strval = strp.c_str();
}
// Conversion:
const char* toChar() {
return strval;
}
string toStr() {
return string(strval);
}
// Functions:
// Input-Output:
// Write
void write(String str)        { printf(str.toChar()); }
void write(const char* str) { printf(str);              }
void write(string str)        { printf(str.c_str());  }
// Read
String read()                       { char str[100]; scanf("%s", str); return String(str); }
//TODO: More to come
// Executing
/*String run(String command) {
const char* cmd = command.toChar();
char buffer[128];
string result = "";
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer, 128, pipe.get()) != NULL)
result += buffer;
}
return String(result);
}*/
String run(String command) {
char buffer[128];
std::string result = "";
const char* cmd = command.toChar();
FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return String(result);
}
}  

我不确定为什么会发生这种情况。我认为这与我如何声明/定义常量字符*"strval"有关。有人可以帮忙吗? PS:如果答案太大,这个项目在Github上:AravK/C-Applications

让我们看一下你的构造函数:

String::String() {
const char* strval = "";
}

这声明了一个名为strval局部变量。变量是构造函数的局部变量;构造函数执行完成后,它就不存在。

相反,您需要的是一个成员变量 - 在类中声明它,但不成员方法或构造函数中声明它。事实上,您已经在头文件中这样定义了它:

class String {
static const char* strval;

因此,从构造函数中删除const char *并添加一个类限定符,以便该行成为现有变量的赋值,而不是创建局部变量:

String::String() {
String::strval = "";
}

并更改给您错误的 return 语句:

return String::strval;

或者也许 -这可能是你真正想要的- 从变量定义中删除static限定符,并将构造函数更改为:

String::String() {
strval = "";
}

此外,析构函数错误地delete不一定是动态分配的数据,或者可能属于另一个对象的数据:

String::~String() {
delete strval;
}

这需要返工。目前最简单的解决方案是完全删除delete strval

您的read()函数可能会通过将 scanf("%s") 与固定大小的缓冲区和未知的输入大小一起使用来引发缓冲区溢出:

char str[100]; scanf("%s", str); return String(str);

最后,您的命令行:

g++ test.cpp sys/Base.h sys/Base.cpp

不应包含头文件 (Base.h)。您正在指定要编译的单位,并且 Base.h 已包含在 Base.cpp 中;它不是一个独立的单元,应该被独立编译。

是的,您没有将类中的变量定义为字段。 构造函数中有 3 个局部变量声明。 只需按照您在标题中的方式添加它即可。

static const char* strval

并删除构造函数中的定义。只需保留分配部分。 问候