提问人:Kalix 提问时间:10/21/2022 最后编辑:Kalix 更新时间:10/21/2022 访问量:57
c++ 中奇怪的命名空间行为
Weird Namespace Behavior in c++
问:
我正在操作系统上工作,在更新字符串标头的实现时(默认情况下操作系统中没有链接 libstd),我开始出现很多错误。
在一个案例中,我的标头字符串包含在一个名为“fat16.h”的标头中,然后包含在“filesystem.h”中。 在构建文件系统.cpp(包括filesystem.h)时,我遇到了此错误。
src/kernel/filesystem/filesystem.h:16:27: error: 'kstd' has not been declared
16 | extern FHANDLE(*fileOpen)(kstd::string path);
是的,字符串标头包含在 filesystem.h 中,并且名称正确。
字符串标头:
#ifndef __K_STRING_H__
#define __K_STRING_H__
#include <functions.h>
#include <screen.h>
#include <kvector>
#include <stddef.h>
namespace kstd {
class string {
private:
char* m_cstr;
size_t m_size;
public:
string() {
m_cstr = new char[1];
m_cstr[0] = '\0';
m_size = 0;
}
string(const string& old) {
m_cstr = new char[old.size() + 1];
memcpy(m_cstr, old.c_str(), old.size() + 1);
m_size = old.size();
}
string(const char* _str) {
m_size = strlen(_str);
m_cstr = new char[m_size+1];
memcpy(m_cstr, _str, m_size+1);
}
void operator+=(const string& _val) {
char* newBuf = new char[_val.size() + m_size + 1];
memcpy(newBuf, m_cstr, m_size);
memcpy(newBuf + m_size, _val.c_str(), _val.size() + 1);
delete[] m_cstr;
m_cstr = newBuf;
m_size += _val.size();
}
string operator+(const string& other) {
char* newBuf = new char[other.size() + m_size + 1];
memcpy(newBuf, m_cstr, m_size);
memcpy(newBuf + m_size, other.c_str(), other.size() + 1);
return newBuf;
}
void operator+=(char val) {
char* newBuf = new char[m_size + 2];
memcpy(newBuf, m_cstr, m_size);
newBuf[m_size] = val;
newBuf[m_size+1] = '\0';
m_size++;
delete[] m_cstr;
m_cstr = newBuf;
}
string& erase(size_t pos = 0, size_t len = 1) {
char* buf = new char[m_size - len + 1];
int a = 0;
for (int i = 0; i < m_size; i++) {
if (i >= pos && i < pos + len) {
continue;
}
buf[a] = m_cstr[i];
a++;
}
m_cstr = buf;
m_size = m_size - len;
return *this;
}
void pop_back() {
erase(m_size-1, 1);
}
void operator=(const char* other) {
m_size = strlen(other);
m_cstr = new char[m_size + 1];
memcpy(m_cstr, other, m_size + 1);
}
void operator=(const string& other) {
m_cstr = new char[other.size() + 1];
memcpy(m_cstr, other.c_str(), other.size() + 1);
m_size = other.size();
}
bool operator== (const string& other) {
// if they both dont have the same size then they cant be equal
if (other.size() != m_size)
return false;
for (int i = 0; i < m_size; i++) {
// if there is ever a character that doesnt match
if (other[i] != m_cstr[i])
return false;
}
return true;
}
char& operator[] (size_t index) {
return m_cstr[index];
}
const char& operator[] (const size_t index) const {
return m_cstr[index];
}
char* c_str() const {
return m_cstr;
}
size_t size() const {
return m_size;
}
~string() {
delete[] m_cstr;
}
};
}
static ostream &operator<<(ostream &out, kstd::string val) {
out.print(val.c_str());
return out;
}
#endif
文件系统.h
#ifndef FILESYSTEM_H
#define FILESYSTEM_H
#include "fat16.h"
// general file handle
#ifndef FHANDLE
#define FHANDLE void*
#endif
enum FileSys {
FAT16
};
// returns a handle to the file that can later be used to do I/O with the file
extern FHANDLE(*fileOpen)(kstd::string path);
// frees all allocated data and should be called after all file IO is done to a file, after this, the file handle becomes invalid and must not be used again
extern void(*fileClose)(FHANDLE file);
// reads from the file handle file, into the buffer buf, with a size of sz, starting from the offset offset
extern void(*fileRead)(FHANDLE file, char* buf, uint32_t size, uint32_t offset);
// returns size of the file
extern uint32_t(*fileSize)(FHANDLE file);
// initalizes whichever file system the computer is running
void initFS();
FileSys getFS();
#endif
包括 fat16.h:
#include <kvector>
#include <kstring>
#include <stdint.h>
#include <functions.h>
#include <diskATA.h>
#include <screen.h>
答: 暂无答案
评论
filesystem.cpp
-E
kvector
#define __K_STRING_H__