为什么我使用以下代码收到错误"可能无法初始化可变大小的对象"?
int boardAux[length][length] = {{0}};
Run Code Online (Sandbox Code Playgroud) 我有一些程序,每次我运行它,它抛出异常,我不知道如何检查究竟是什么抛出,所以我的问题是,是否有可能捕获异常并打印它(我发现行抛出异常)谢谢提前
我对这两个术语有点困惑,有人可以解释一下有什么区别,例如Eclipse是IDE的一个例子,在那里我可以编辑,调试,编译我的程序,但是我可以用SDK做同样的事情,我错了吗?提前致谢
我有一些技术问题.在这个功能:
string report() const {
if(list.begin() == list.end()){
throw "not good";
}
//do something
}
Run Code Online (Sandbox Code Playgroud)
如果我抛出异常程序正在发生什么?我的功能会终止还是会进一步运行?如果它终止,它将返回什么价值?
有人可以解释我的错误,我有这个课:
class Account
{
private:
string strLastName;
string strFirstName;
int nID;
int nLines;
double lastBill;
public:
Account(string firstName, string lastName, int id);
friend string printAccount(string firstName, string lastName, int id, int lines, double lastBill);
}
Run Code Online (Sandbox Code Playgroud)
但是当我打电话给它时:
string reportAccounts() const
{
string report(printAccountsHeader());
for(list<Account>::const_iterator i = listOfAccounts.begin(); i != listOfAccounts.end(); ++i)
{
report += printAccount(i->strFirstName, i->strLastName, i->nID, i->nLines, i->lastBill);;
}
return report;
}
Run Code Online (Sandbox Code Playgroud)
我收到错误within context
,有人可以解释原因吗?
我对组件一些问题,就是有可能将数字转换的ASCII码(0,1,2,3,4,5,6,7,8,9),而不10 compares and branches
像
if(input == 48) return 0;
...
Run Code Online (Sandbox Code Playgroud)
(我正在谈论关于汇编的代码!)提前感谢
我有这段代码
#include "account.h"
#include <iostream>
#include <string>
using namespace std;
Account::Account(string firstName, string lastName, int id)
: strFirstName(firstName), strLastName(lastName), nID(id) {}
void Account::printAccount(){
cout << strFirstName;
}
Run Code Online (Sandbox Code Playgroud)
#include <string>
using std::string;
class Account{
private:
string strLastName; //Client's last name
string strFirstName; //Client's first name
int nID; //Client's ID number
int nLines; //Number of lines related to account
double lastBill;
public:
Account(string firstName, string lastName, int id);
void printAccount();
};
Run Code Online (Sandbox Code Playgroud)
#ifndef CELLULAR_COMPANY_H
#define CELLULAR_COMPANY_H
#include <string>
#include <list> …
Run Code Online (Sandbox Code Playgroud) 有人可以在课堂上解释私人和公共秩序.它重要与否?
例如:
class Account{
public:
Account(string firstName, string lastName, int id);
void printAccount();
private:
string strLastName;
string strFirstName;
};
Run Code Online (Sandbox Code Playgroud)
它会是一样的:
class Account{
private:
string strLastName;
string strFirstName;
public:
Account(string firstName, string lastName, int id);
void printAccount();
};
Run Code Online (Sandbox Code Playgroud) 我有这个代码片段:
void addLineRelative(LineNumber number, LineNumber relativeNumber) {
list<shared_ptr<Line> >::iterator i;
findLine(i, number);
if(i == listOfLines.end()){
throw "LineDoesNotExist";
}
line 15 if(dynamic_cast<shared_ptr<FamilyLine> >(*i)){
cout << "Family Line";
} else {
throw "Not A Family Line";
}
}
Run Code Online (Sandbox Code Playgroud)
我有类 Line 并从它派生 FamilyLine 和 RegularLine,所以我想找到 FamilyLine
我的程序在第 15 行失败,我收到一个错误
cannot dynamic_cast target is not pointer or reference
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗,提前致谢
我试过这个:
shared_ptr<FamilyLine> ptr(dynamic_cast<shared_ptr<FamilyLine> >(*i));
if(ptr){
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
同样的错误
void addLineRelative(LineNumber number, LineNumber relativeNumber) {
list<shared_ptr<Line> >::iterator i;
findLine(i, number);
if(i == listOfLines.end()){ …
Run Code Online (Sandbox Code Playgroud) 我有一些技术问题,我在工作中重复了代码,我想摆脱它,所以我知道在C++中使用宏不是一个好主意,而是我必须使用内联函数,这是个好主意将此函数用作内联:
list<Data>::iterator foo(int data){
if(dataExists(data)){
list<Data>::iterator i;
for(i = dataClass.begin(); i != dataClass.end(); ++i){
if(i->getData() == data){
break;
}
return i; //here I have one more problem, what can I return if data doesn't exist?
}
Run Code Online (Sandbox Code Playgroud)
我是初学者,我认为这个功能非常不安全,有人可以给我建议,我怎样才能改进我的代码,提前谢谢
PS通常用什么来避免重复代码?