我从visual studio获得了一些奇怪的行为,关于以下代码片段,错误列表显示了E0349的几个实例:没有运算符"[]"匹配这些操作数.
Intellisense似乎暗示了类型不匹配,但正如您将在代码中看到的那样,没有类型不匹配.
首先,为了制作mat4,我已经定义了一个Vec4结构:(我只包含了相关的功能)
struct Vec4f
{
union
{
struct { float x, y, z, w; };
struct { float r, g, b, a; };
};
// copy constructor
Vec4f(Vec4f const & v) :
x(v.x),
y(v.y),
z(v.z),
w(v.w)
{
}
// Operators
float & operator[](int index)
{
assert(index > -1 && index < 4);
return (&x)[index];
}
Vec4f & operator=(Vec4f const & v)
{
// If I use: "this->x = v[0];" as above, E0349
this->x = v.x;
this->y = v.y; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用更短的语法并避免std::在任何地方使用,所以我开始使用新的别名语法.在某些例子中,我看到人们以这种方式使用它:
using json = nlohmann::json;
Run Code Online (Sandbox Code Playgroud)
并尝试使用std::,但代码如下:
#include <iostream>
using cout = std::cout;
int main()
{
cout << "Sometext";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我得到错误'cout' in namespace 'std' does not name a type.我知道我可以使用
using std::cout;
Run Code Online (Sandbox Code Playgroud)
但为什么using cout = std::cout;不起作用?
编辑:
所有投票结束这个问题的人:我发布了它,因为我无法找到解决方案,通过写错误信息.是的,提到的问题是解决我的问题的问题描述了会发生什么,但是当有人遇到这种错误时,他将无法轻易找到解决方案.我只是没有意识到,那cout是一个对象.我已经读过这样的一些问题,但仍然不知道会发生什么.
在作用域中创建临时对象时,编译器不会看到构造函数提供的参数并给出编译错误.
如下例所示:
#include <sstream>
#include <iostream>
class csventry
{
public:
csventry(std::ostream& ostream) :
_ostream(ostream)
{}
~csventry()
{ _ostream << std::endl; }
csventry& operator << (const std::string& value) {
_ostream << ", ";
_ostream << value;
return *this;
}
private:
std::ostream& _ostream;
};
int main ()
{
std::ostringstream log;
{ csventry(log) << "User Log-on:"; }
{ csventry(log); }
{ csventry(log) << "Summary:"; }
std::cout<<log.str()<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误.
main.cpp: In function ‘int main()’:
main.cpp:29:16: error: no matching function for call to ‘csventry::csventry()’
{ …Run Code Online (Sandbox Code Playgroud) 我找到了一些无法理解的东西.我认为它应该与函数堆栈和一些未定义的行为有关.
假设我有一个功能工厂模板(傻一个):
template <unsigned int N=10>
std::function<int&&(const int& n)> build_add_function() {
return [](const int& n) -> int&& {std::move(n+N);};
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,它缺少非void函数的return语句,因此编译器向我发出警告......奇怪的是它"按预期"工作
int main() {
auto foo = build_add_function();
std::cout << foo(10);
}
Run Code Online (Sandbox Code Playgroud)
主要输出: 20
当然,为了修复代码,我添加了return语句,它给了我一个分段错误
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Run Code Online (Sandbox Code Playgroud)
我对我正在做的事情有一些误解,但我无法理解它.有人会向我解释这里发生了什么吗?我正在使用gcc版本8.0.1
编辑:刚刚测试gcc 4.8.1并使用return语句按预期工作,没有编译错误.
它是编译器的东西吗?
在C++中,如果:
int a = 3;
int* p = &a;
Run Code Online (Sandbox Code Playgroud)
那为什么呢
const int* &pp = p;
Run Code Online (Sandbox Code Playgroud)
不允许,但是
const int* const &pp = p;
Run Code Online (Sandbox Code Playgroud)
被允许?
我有一个结构,里面是一个指向同一结构的函数的指针.现在我需要调用一个指向结构外部函数的指针.我举一个下面的代码示例:
#include <iostream>
struct test {
void (test::*tp)(); // I need to call this pointer-to-function
void t() {
std::cout << "test\n";
}
void init() {
tp = &test::t;
}
void print() {
(this->*tp)();
}
};
void (test::*tp)();
int main() {
test t;
t.init();
t.print();
(t.*tp)(); // segfault, I need to call it
return 0;
}
Run Code Online (Sandbox Code Playgroud) 检查以下代码:
#include <iostream>
using namespace std;
int& foo() {
static int i = 0;
return i;
}
int main() {
cout << &foo() << endl;
cout << &foo << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正如你看到的,第一个cout返回值的打印地址foo(),这将是静态变量i里面foo().对于第二个cout我期待那&foo的回报地址foo()功能,如规定在这里:
2)如果操作数是非静态成员的限定名,例如&C :: member,则结果是指向成员函数的prvalue指针或指向类C中类型T的数据成员的指针.注意,&member和C都不是: :member甚至&(C :: member)可用于初始化指向成员的指针.
但令我惊讶的是,这是我的输出:
0x5650dc8dc174
1
Run Code Online (Sandbox Code Playgroud)
第一个是好的,但第二个是1?怎么回事?为了确保我没有弄乱任何东西,我在下面编写了这段代码C:
#include <stdio.h>
int foo() {
}
int main(void) {
printf("%p", &foo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下输出:
0x55732bd426f0 …Run Code Online (Sandbox Code Playgroud) 为什么我需要在线*上制作checker指针
template <typename C> static yes test( checker<C, &C::helloworld>* );
为了使编译时间扣除正常工作,输出1 0?
当我删除时*,输出是0 0
#include <iostream>
struct Generic {};
struct Hello
{ int helloworld() { return 0; } };
// SFINAE test
template <typename T>
class has_helloworld
{
typedef char yes;
typedef struct {char _[2];} no;
template <typename C, int (C::*)()> struct checker;
template <typename C> static yes test( checker<C, &C::helloworld>* );
template <typename C> static no test(...);
public:
enum { …Run Code Online (Sandbox Code Playgroud) 在下面的例子中,我有一个智能指针sp作为成员变量,我想在构造函数中为它分配一个动态分配的数组,但是我得到一个编译器错误no match for ‘operator=’,这是做什么的正确方法?
在下面的例子中,我有一个智能指针sp作为成员变量,我想在构造函数中为它分配一个动态分配的数组,使用reset()智能指针的方法正确的方法来做到这一点,或者我应该使用共享智能指针?
struct SampleStructure
{
std::unique_ptr<idx_t[]> sp;
SampleStructure(int a, int b){
sp.reset(new idx_t[a + 1]);
}
};
Run Code Online (Sandbox Code Playgroud) 这是我的问题,我有一个基类Base、两个派生类ClassA和仅采用两个参数的ClassB形式Base,以及两个类ClassB0并ClassB1从ClassB采用三个参数派生而来。我想根据传递给函数的类型创建类实例create,如果类型派生自ClassB,则第一个参数将填充为 10。编译器总是警告没有匹配的构造函数。
#include <iostream>
#include <type_traits>
class Base {};
class ClassA : public Base {
public:
ClassA(int a, int b) : Base() {
std::cout << "ClassA: " << a << " " << b << "\n\n";
}
};
class ClassB : public Base {
public:
ClassB(int a, int b, int c) : Base() {
std::cout << "ClassB: " << a << " …Run Code Online (Sandbox Code Playgroud)