我一直认为基类的公共方法确实被派生类继承,甚至认为派生类没有定义该特定方法.例如
#include <iostream>
using namespace std;
class A {
public:
int f() { cout << 3; return 0;}
int f(int x) {cout << x; return 0;}
};
class B: public A {
public:
int f() {std::cout << 5; return 0;}
};
int main(){
B ob;
ob.f(7);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期待结果是:7,但我得到编译错误说
" 错误:函数调用的参数太多,预期为0,有1;你的意思是'A :: f'吗? "
我知道错误试图说的是什么,但我很困惑,没有调用Base类的功能.
我想说我上课了
class A{
public:
struct stuff{
int x;
int y;
};
stuff get_stuff(int index){
if(index < vec_stuff.size() && index >= 0) return vec_stuff[index];
else return nullptr;
}
std::vector<stuff> vec_stuff;
};
Run Code Online (Sandbox Code Playgroud)
假设我vec_stuff恰当地填充了这些.
然后在其他一些课堂上
#inclde "A.h"
class B{
public:
B(){}
void f(int index, int k){
xxxx = a.get_stuff(index)
}
A a; // assume A is initialized properly
}
Run Code Online (Sandbox Code Playgroud)
所以在我写xxxx在那里的地方应该是,struct stuff但是当我这样做时,我得到了use of undeclared identifier如何让编译器知道stuff我所指的是在A里面.
我试图理解单例设计模式并创建了一个最简单的模式:
#include <iostream>
class mySingleton{
private:
static mySingleton *ptr;
mySingleton(){ }
public:
static mySingleton* getInstance(){
if(!ptr){
ptr = new mySingleton();
return ptr;
} else return ptr;
}
void msg(){
std::cout << " Hello World!! " << std::endl;
}
};
int main(){
mySingleton* obj = mySingleton::getInstance();
mySingleton* obj2 = mySingleton::getInstance();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,我得到:
Undefined symbols for architecture x86_64:
"mySingleton::ptr", referenced from:
mySingleton::getInstance() in ccm822LI.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
为什么我不能在静态函数中使用ptr,因为ptr也是一个静态变量?我在这里错过了什么吗?
假设我不想使用向量并使用动态数组(只是一个学习目的).从我分配的这个动态数组我想删除特定的索引.
int * array = new int[10];
delete &array[3];
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时,我得到了SIGSEGV,Segmentation fault.
我如何删除特定的索引(比如索引3,我知道它的连续内存,它会创建漏洞,但仅仅是为了学习目的),就像我正在尝试正确地做上面那样,如果这是我的记忆,为什么我会出现分段错误?
#include <iostream>
int main() {
int arr[2] = {1, 2};
int *p;
// p = &arr; // Does not compile
p = &arr[0]; // compiles
std::cout << "&arr = " << &arr << std::endl;
std::cout << "&arr[0] = " << &arr[0] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试打印地址时,两者都打印相同的地址。但是当我尝试分配它时p = &arr,它无法编译。标准中是否有一些内容反对将数组地址分配给指针。我只是想知道为什么 p = &arr不能编译?
铿锵实际上说error: cannot initialize a variable of type 'int *' with an rvalue of type
我试图使用括号运算符重载getter和setter.当我尝试使用时
double& my_vector::operator()(size_t index){
return array[index]
}
Run Code Online (Sandbox Code Playgroud)
这个单个运算符重载适用于这两种情况:
例如:my_vector(1) = 4.01;&double x = my_vector(1);
但是现在我想让getter as const和setter,non-const因为我将在另一个const函数中使用const getter.我做
得到的 double& my_vector::operator()(size_t index) const;
二传手 double& my_vector::operator()(size_t index);
我得到错误:只有它们的返回类型不同的函数不能重载.
我想我知道错误在说什么,但我不确定如何纠正它.
我有一个结构
struct Key {
double x;
double y;
double z;
bool operator==(const Key& k) const{
return (x == k.x && y == k.y && z == k.z);
}
}
Run Code Online (Sandbox Code Playgroud)
我这样做是因为我想把它作为哈希映射的关键.
然后我做
std::unordered_map<Key, int> map = {{1.01, 2.02, 3.03}, 333};
Run Code Online (Sandbox Code Playgroud)
我想使用初始化列表作为构造函数,但我得到错误 no matching constructor for initialization of 'std::unordered_map<key, int> map = {{1.01, 2.02, 3.03}, 333};'