我是一个 C++ 初学者。我发现了一个奇怪的现象。GDB 无法给出此代码中错误根本原因的行号。
#include <array>
using std::array;
int main(int argc, char **argv) {
array<double, 3> edgePoint1{0, 0, 0};
array<double, 3> edgePoint2{0, 0, 0};
array<double, 3> edgePoint3{0, 0, 0};
array<array<double, 3>, 3> edgePoints{};
edgePoints[0] = edgePoint1;
edgePoints[1] = edgePoint2;
edgePoints[3] = edgePoint3;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第 13 行是问题的根源。但是当我在 GBD 中使用“bt”时,它会打印第 15 行。为什么?
Program received signal SIGABRT, Aborted.
0x00007f51f3133d7f in raise () from /usr/lib/libc.so.6
(gdb) bt
#0 0x00007f51f3133d7f in raise () from /usr/lib/libc.so.6
#1 0x00007f51f311e672 in abort () from /usr/lib/libc.so.6
#2 …Run Code Online (Sandbox Code Playgroud) 我如何发现 aMultimap已经包含特定值,以及如何查找包含相同键的项目总数?
std::multimap<float,int> obj;
obj.insert ( std::pair<char,int>('a',100) );
obj.insert ( std::pair<char,int>('a',100) );
Run Code Online (Sandbox Code Playgroud)
例如,如果我想检查给定的multimapobj 是否已包含 100 个值,我该如何检查它以及如何获取包含字符 a 的项目数为 2?
我试图找出代码中编译错误的原因:
class A
{
public:
virtual ~A(){}
};
class B: public A
{
public:
virtual ~B(){}
};
class D: public B
{
public:
virtual ~D(){}
};
template <class X, class Y>
X* fun(X* p){return dynamic_cast<Y*>(p);}
int main()
{
A* q = dynamic_cast<B*>(new D());
A* p = fun<D,B>(new D());
}
Run Code Online (Sandbox Code Playgroud)
对我来说,似乎指针q和p应该指向相同的类型但是对于p我收到编译器错误,说"无效转换从'B*'到'D*'".我唯一没有得到错误的是当我以B的子类为D(因此p是空指针)的方式更改类时.谁能帮我理解为什么会这样?
如果 sizeof(struct ...) 不等于给定数字,如何获取 C 编译时#error?
问题来自编程课程,我想避免运行错误大小的二进制代码。
(众所周知,sizeof 运算符在 #if .. #endif 指令中不起作用。)
我知道我必须为std :: set重载运算符<。
我用两个类重载了运算符<:UniqueID和UniqueIDWithBug。唯一的区别是this->unique_id_a_ == t.unique_id_a_比较时添加了“ UniqueID”代码。
然后,我将相同的元素放入两组。最后,我在集合中找到了一个元素。一组可以找到它,另一组找不到。这个问题使我困惑了很长时间。
struct UniqueID {
uint64_t unique_id_a_{0};
uint64_t unique_id_b_{0};
bool operator<(const UniqueID &t) const {
if (this->unique_id_a_ < t.unique_id_a_) {
return true;
}
if (this->unique_id_a_ == t.unique_id_a_ &&
this->unique_id_b_ < t.unique_id_b_) {
return true;
}
return false;
}
};
struct UniqueIDWithBug {
uint64_t unique_id_a_{0};
uint64_t unique_id_b_{0};
bool operator<(const UniqueIDWithBug &t) const {
if (this->unique_id_a_ < t.unique_id_a_) {
return true;
}
return (this->unique_id_b_ < t.unique_id_b_);
}
};
// init data
std::set<UniqueID> _set = …Run Code Online (Sandbox Code Playgroud) 我是C ++的新手,目前正在从事某些项目,并希望使用C ++而不是C。
我遇到的第一个问题是,例如在OpenSSL中,有接受char*参数的函数。
在C ++中,使用不是一个好主意char*。我读过有人建议std::string或std::vector<char>代替。
但是例如OpenSSL中的BIO_read(将数据写入char*)函数accepts char*。std::string具有功能,c_str()但返回const char*。我知道我可以const使用const_cast强制转换,但这不是一个好主意,因为这不是应该更改字符串的方式。
这个问题的“ C ++解决方案”是什么?我想同时使用RAII和OOP原则。我唯一想到的解决方案是创建一个类,该类将在构造函数中接受内存大小作为参数,并具有类似这样的内容char* _buf = new char[size]并在析构函数中释放内存。这是针对这种情况的最佳解决方案吗?
还是在我recv不知道大小的情况下应该使用从套接字接收的数据呢?在CI中,将使用分配内存malloc并将其写入那里。但是,我该如何以“ C ++风格”做到这一点?创建我上面提到的类,并使用它代替malloc?
我想从FILE读取数据并将其保存到链接列表,而我的问题显然是由读取命令“ fscanf”引起的。
我正在尝试制作一个函数,该函数接收链表的头和指向文件的指针。该函数从文件中读取数据并将其保存到节点中,然后将该节点连接到链接列表的开头,即,链接到开头,而不是结尾。
#include <stdio.h>
#include <stdlib.h>
#define NameLength 15
typedef struct Product {
char ProductName[NameLength];
int Quantity;
int Price;
char Premium;
}Product;
typedef struct ProductList {
Product P;
struct ProductList* next;
}ProductList;
void DeleteList(ProductList*);
void ErrorMsg(const char*);
int CheckInList(ProductList*, const char*);
void CreateProducts(ProductList *head, FILE *fp) {
ProductList *temp = (ProductList*)malloc(sizeof(ProductList));
//If the dynamic memory allocation for temp has failed, print a
message and exit the program.
if (!temp) {
DeleteList(head);
ErrorMsg("Error: Memory allocation of temp in CreateProducts has …Run Code Online (Sandbox Code Playgroud) 我为自己设定的目标是重载operator+(添加类对象)。事实证明,该总和可以解释为两个向量的总和。但是当涉及到该方法时operator+,我发现很难返回该对象。我读过类似的主题,甚至尝试应用一些建议,但不幸的是没有成功。我附上一些代码。
template<class Y>
class myVect {
public:
myVect(int n = 1);
~myVect();
myVect(const myVect& a);
myVect& operator= (const myVect&);
myVect& operator+ (const myVect&);
void display(const myVect& a);
private:
int size;
Y* data;
template<class U> friend class myClass;
};
template<class Y> // constructor
myVect<Y>::myVect(int n) {
size = n;
data = new Y[size];
cout << endl << "Pass the elements" << " " << size << "\n";
for (int i = 0; i < size; i++) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个程序来计算一个字母在字符串中的次数,但是下面的程序没有输出任何内容,尽管该函数count()确实被调用了。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int count(const string &s, char c) {
string::const_iterator i = find(s.begin(), s.end(), c);
int n = 0;
while (i != s.end()) {
++n;
i = find(i+1, s.end(), c);
}
return n;
}
int main() {
const string e = "dddddddd";
char d = 'd';
count(e, d);
}
Run Code Online (Sandbox Code Playgroud) 我已经开始在 Ubuntu 上学习 C++。我也只有几个月才开始使用 Linux。
我正在尝试将 2D Ball Collision Script 从 Javascript 移植到 C++ 以用于学习目的。
我在 C++ 中使用 simple2D 进行绘图:https : //github.com/simple2d/simple2d
我去运行这个命令:
simple2d build c-code-test.cpp
Run Code Online (Sandbox Code Playgroud)
我收到这个回复:
cc1plus: warning: command line option ‘-std=c11’ is valid for C/ObjC but not for C++
/usr/bin/ld: /tmp/ccl07DBG.o: undefined reference to symbol '_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4'
//usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
由于我对 Linux 和 C++ 的熟悉程度,我无法根据之前关于堆栈溢出的问题做出正确的推断来解决这个问题。我已经安装了 libstdc++6,所以尽管它会正确链接,但我还是会这样做。
有人可以指导我完成第 1、2、3 步吗……拜托吗?非常感谢你!