有没有什么好的理由在C++中调用默认构造函数时,一组空的圆括号(括号)无效?
MyObject object; // ok - default ctor
MyObject object(blah); // ok
MyObject object(); // error
Run Code Online (Sandbox Code Playgroud)
我似乎每次都自动输入"()".是不是有一个很好的理由不允许这样做?
c++ constructor c++-faq default-constructor most-vexing-parse
我刚遇到这个问题
error: request for member ‘show’ in ‘myWindow’, which is of non-class type ‘MainGUIWindow()’
Run Code Online (Sandbox Code Playgroud)
当试图编译一个简单的qt应用程序时:
#include <QApplication>
#include "gui/MainGUIWindow.h"
int main( int argc, char** argv )
{
QApplication app( argc, argv );
MainGUIWindow myWindow();
myWindow.show();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
我通过替换解决了这个问题
MainGUIWindow myWindow();
Run Code Online (Sandbox Code Playgroud)
通过
MainGUIWindow myWindow;
Run Code Online (Sandbox Code Playgroud)
但我不明白其中的区别.我的问题:有什么区别?
此致,德克
可能重复:
为什么没有调用构造函数?
我正在使用Visual Studio 2012,假设Test是一个类
class Test
{
};
Run Code Online (Sandbox Code Playgroud)
当我创建一个新的Test实例时,以下两种方式的区别是什么?
方式1
Test t;
Run Code Online (Sandbox Code Playgroud)
方式2
Test t();
Run Code Online (Sandbox Code Playgroud)
我在下面的代码中得到了这个问题,最初,我在方法2中定义了A的实例,我只得到一个错误,因为B没有提供默认构造函数,但是当我以方式1定义它时,我得到了一个额外的错误.
class B
{
B(int i){}
};
class A
{
A(){}
B b;
};
int main(void)
{
A a(); // define object a in way 2
getchar() ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
如果我定义一个方式1
A a;
Run Code Online (Sandbox Code Playgroud)
我会得到另一个错误说
错误C2248:'A :: A':无法访问在类'A'中声明的私有成员
所以我猜两种方式之间肯定存在一些差异.
我找到了一些我不理解的东西.
std::string a();
Run Code Online (Sandbox Code Playgroud)
打印出来后返回1
.我不知道它来自哪里.我认为a()
是一个没有参数的构造函数,但它看起来不是.
我在哪里可以找到有关此信息?这是什么?
当试图做std::string b(a);
编译器呼喊时:
error: no matching function for call to ‘std::basic_string<char>::basic_string(std::string (&)())’
Run Code Online (Sandbox Code Playgroud)
解释将不胜感激.
#include<iostream>
using namespace std;
class base {
public:
base() {
cout<<"Constructing base \n";
}
virtual ~base() {
cout<<"Destructing base \n";
}
};
class derived: public base {
public:
derived() {
cout<<"Constructing derived \n";
}
~derived() {
cout<<"Destructing derived \n";
}
};
int main(void) {
derived d();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么在这个程序中它没有调用构造函数?
谁能解释一下?
.......
我有一段时间使用C的经验,一般来说使用OOP的时间较少,但现在才开始尝试学习C++.这是对模板的练习.
我有一个简单的课程:
struct type {
type(double data = 0) : data(data)
{
std::cout << "at type constructor" << std::endl;
}
friend std::ostream& operator<<(std::ostream& out, const type &that)
{
return out << "[" << std::setfill('0') << std::setw(7) <<
std::fixed << std::setprecision(2) << that.data << "]";
}
private:
double data;
};
Run Code Online (Sandbox Code Playgroud)
一个基本模板:
template<typename T>
struct mat4 {
T a1, a2, a3, a4;
T b1, b2, b3, b4;
T c1, c2, c3, c4;
T d1, d2, d3, d4;
mat4 (T a1 …
Run Code Online (Sandbox Code Playgroud) 在这两个程序中,第二个程序可以工作,但第一个程序不能编译.怎么可能?唯一的区别是,在版本2中,bar是指针,而在版本1中,它不是.
版本一:(不编译)
#include <iostream>
class Foo{
public:
void print(){
std::cout << "asdasd" << std::endl;
}
};
class Bar : public Foo{
};
int main(){
Bar bar();
bar.print();
}
Run Code Online (Sandbox Code Playgroud)
第二个版本:
#include <iostream>
class Foo{
public:
void print(){
std::cout << "asdasd" << std::endl;
}
};
class Bar : public Foo{
};
int main(){
Bar* bar = new Bar();
bar->print();
}
Run Code Online (Sandbox Code Playgroud) 当我收到来自intelisense的错误时,我正在编写一个小游戏.我再次尝试了一个简单的宠物类,但intellisense认为它不正确.这是代码和错误:
#include <iostream>
class Pet{
public:
int m_hunger;
void Greet();
};
void Pet::Greet(){
std::cout << "My hunger is " << m_hunger;
}
int main(){
Pet dog();
dog.m_hunger = 9;//Expression must have class type
dog.Greet();//Expression must have class type
return 0;
}
Run Code Online (Sandbox Code Playgroud)