使用g ++ 4.6编译的以下程序会产生错误
request for member ‘y’ in ‘a2’, which is of non-class type ‘A<B>(B)’
Run Code Online (Sandbox Code Playgroud)
在最后一行:
#include <iostream>
template <class T> class A
{
public:
T y;
A(T x):y(x){}
};
class B
{
public:
int u;
B(int v):u(v){}
};
int main()
{
int v = 10;
B b1(v);
//works
A<B> a1(b1);
//does not work (the error is when a2 is used)
A<B> a2(B(v));
//works
//A<B> a2((B(v)));
std::cout << a1.y.u << " " << a2.y.u << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
从代码中包含的工作变量可以看出,在A的构造函数的参数周围添加括号可以解决问题.
我已经看到一些由构造函数调用解释为函数声明引起的相关错误,比如在创建一个没有参数的构造函数的对象时,但是使用大括号:
myclass …Run Code Online (Sandbox Code Playgroud) 标题来自Marshall Cline 着名的网站C++ FAQ.
作者声称以下两个代码示例之间存在差异.
假设List是某个类的名称.然后函数f()声明一个名为x的本地List对象:
void f()
{
List x; // Local object named x (of class List)
...
}
Run Code Online (Sandbox Code Playgroud)
但是函数g()声明了一个名为x()的函数,它返回一个List:
void g()
{
List x(); // Function named x (that returns a List)
...
}
Run Code Online (Sandbox Code Playgroud)
但使用第二种变体是否真的错了?
如果真的是一个声明,编译器会不会抱怨你不能在函数中声明一个函数?
如何让编译器创建临时代码,在不定义函数时使用默认构造函数?
struct B {};
template<class T> struct C {};
template<class T,class T1>
struct A
{
A(const T& t,const T1& t1): m_t(t),m_t1(t1)
{
std::cout << __PRETTY_FUNCTION__ << "\n";
}
T m_t;
T1 m_t1;
};
int main() {
A< B , C<B> > a0( B() , C<B>() ); // Function definition
A< B , C<B> > a1( B b , C<B> c ); // dito, *at least A(const T& t,const T1& t1) not called
}
Run Code Online (Sandbox Code Playgroud) 我对C++很陌生并观察到,以下代码行的行为不同
MyClass c1;
c1.do_work() //works
MyClass c2();
c2.do_work() //compiler error c2228: left side is not a class, structure, or union.
MyClass c3{};
c3.do_work() //works
Run Code Online (Sandbox Code Playgroud)
头文件为
class MyClass {
public:
MyClass();
void do_work();
};
Run Code Online (Sandbox Code Playgroud)
你能解释一下,创建对象的三种方式之间的区别是什么?为什么第二种方式会产生编译错误?
对于看到这个问题的人:看看答案并考虑使用:cdecl
为什么下面的代码会给出编译器错误:
prog.cpp: In function ‘int main()’:
prog.cpp:23:4: error: request for member ‘size’ in ‘a’, which is of non-class type ‘RangeVec<int>(RangeVec<int>)’
a.size();
^
Run Code Online (Sandbox Code Playgroud)
我不明白这段代码有什么问题?
#include <iostream>
#include <vector>
template<typename Type>
class RangeVec: public std::vector<Type> {
public:
RangeVec( const RangeVec & v): std::vector<Type>(v){}
RangeVec( const RangeVec && v): std::vector<Type>(std::move(v)) {}
RangeVec( const std::vector<Type> &v)
: std::vector<Type>(v)
{
//std::sort(std::vector<Type>::begin(),std::vector<Type>::end());
}
};
int main(){
std::vector<int> v;
RangeVec<int> b(v);
RangeVec<int> a( RangeVec<int>(b) );
a.size(); // b.size() works ???? why??
}
Run Code Online (Sandbox Code Playgroud) 我有一个类A接受B类作为构造函数参数.B类可以用int值构造.我原来的代码非常复杂,但我希望我把它简化为基本情况:
class B {
public:
explicit B(int a) : val(a) {}
private:
int val;
};
class A {
public:
A(const B & val) : value(val) {};
void print() {
//does nothing
}
private:
B value;
};
int main() {
int someTimeVar = 22;
A a(B(someTimeVar));
a.print();
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误代码:
$ g++ test.cpp -Wall -O0
test.cpp: In function ‘int main()’:
test.cpp:22:7: error: request for member ‘print’ in ‘a’, which is of non-class type ‘A(B)’
a.print();
^
test.cpp:20:9: warning: unused variable ‘someTimeVar’ [-Wunused-variable] …Run Code Online (Sandbox Code Playgroud) 想象一下我有一个类型:
struct my_type
{
double operator()(int a)
{
return 3.1415;
}
};
Run Code Online (Sandbox Code Playgroud)
然后我想把它包起来std::function.考虑两种不同的方法:
my_type m_t;
std::function<double(int)> f(std::move(m_t));
std::cout << f(4) << std::endl;
Run Code Online (Sandbox Code Playgroud)
如我所料,一切都很好,打印出PI的第一个数字.然后是第二种方法:
std::function<double(int)> ff(my_type());
std::cout << ff(4) << std::endl;
Run Code Online (Sandbox Code Playgroud)
在我看来,这个代码是完全一样的第一个.rvalue作为function包装器的参数传递.但问题是,第二个代码不能编译!我真的不知道为什么会这样.
考虑这个非常简单的代码:
#include <memory>
class Foo
{
public:
Foo() {};
};
class Bar
{
public:
Bar( const std::shared_ptr<Foo>& foo ) {}
};
int main()
{
Foo* foo = new Foo;
Bar bar( std::shared_ptr<Foo>( foo ) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么Visual Studio会报告
warning C4930: 'Bar bar(std::shared_ptr<Foo>)': prototyped function not called (was a variable definition intended?)
Run Code Online (Sandbox Code Playgroud)
并且没有bar创建对象......如何将此行Bar bar( std::shared_ptr<Foo>( foo ) );解释为函数定义?
我检查了类型名称与new之后的括号吗?还有C++:警告:C4930:未调用原型函数(是一个变量定义?),但我觉得我的问题不同,因为我没有使用语法Foo()也没有Bar().
编辑:请注意,它成功编译:
Foo* foo = new Foo;
std::shared_ptr<Foo> fooPtr( …Run Code Online (Sandbox Code Playgroud)