小编gim*_*mpf的帖子

捕获最佳实践的异常(c#/ .net)

如果我有这样的代码:

void a()
{
    try
    {
        b();
    }
    catch (MyException)
    {
        // Handle any problems that occurred in b(c(d()))
    }
}

void b()
{
    c();
    // Do something else
}

void c()
{
    d();
    // Do something else
}

void d()
{
    // Do something, throw a MyException if it fails
}
Run Code Online (Sandbox Code Playgroud)

假设在任何时候都不需要清理,最好在c()中调用d()和在b()中调用c()时调用try {} catch {throw;},还是认为OK让d()的异常在没有任何介入的try/catch块的情况下自然地冒泡到a()"自然"?

我认为额外的try/catch块可以作为一种"文档",但它们似乎是多余的,所以我只是想知道其他人会认为最好的方法.

对不起,如果这有点太基础了,我试图了解异常,但我似乎对它们没有好感觉.

c# exception

5
推荐指数
1
解决办法
2556
查看次数

如何修复C++编译器错误"无法将'Type'转换为'const Type*'"?

这是完整的错误消息:

错误:无法将'MyTime'转换为'const MyTime*'以将参数'1'转换为'int DetermineElapsedTime(const MyTime*,const MyTime*)'|

这是我的代码:

#include <iostream>
#include<cstdlib>
#include<cstring>

using namespace std;
struct MyTime { int hours, minutes, seconds; };
int DetermineElapsedTime(const MyTime *t1, const MyTime *t2);
const int hourSeconds = 3600;
const int minSeconds = 60;

int DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{
    long timeDiff = ((((t2->hours * hourSeconds) + (t2->minutes * minSeconds) + t2->seconds) -
                   ((t1->hours * hourSeconds) + (t1->minutes * minSeconds) + t1->seconds)));
    return(timeDiff);
}


int main(void)
{
    char delim1, delim2;
    MyTime tm, tm2; …
Run Code Online (Sandbox Code Playgroud)

c++ constructor pointers compiler-errors

5
推荐指数
1
解决办法
2万
查看次数

无法将参数1从'std :: _ Vector_iterator <_Myvec>'转换为'std :: _ Vector_iterator <_Myvec>'

我有一个问题,它有点难以描述,所以请对我这么容易.

我有两个类,A和B,A类有一个私有成员 - 矢量:

class A
{
private:
    struct complex
    {
       int x;
       vector< int > y;
    };

    vector< complex > m_resultVector; // <---- the private member

public:
    void getPointerToVector( vector< complex >::iterator it )
    {
        it = m_resultVector.begin();
    }
};
Run Code Online (Sandbox Code Playgroud)

我需要从类B获取访问权限(仅读取),对此m_resultVector;,我可以编写一个get函数,但是m_resultVector非常长的向量,我不想将整个向量复制到新的向量,我想发送它的指针.也是重要的一部分 - 我需要B级不能改变的内容m_resultVector

class B
{
    struct complex
    {
        int x;
        vector< int > y;
    };

    void functionOf_B()
    {
        A class_A;
        vector< complex >::iterator p_resultVector;

        class_A.getPointerToVector(p_resultVector); // <------ compilation error here

        // …
Run Code Online (Sandbox Code Playgroud)

c++ vector

3
推荐指数
1
解决办法
2601
查看次数

C++类依赖项

我的班级遇到了一些问题,因为它们彼此依赖,如果没有宣布另一个,就不能宣布.

class block: GtkEventBox {

    public:
        block(board board,guint x,guint y): image("block.png") {
            this.board = board;
            this.x = x;
            this.y = y;
            board.attach(this,x,y,x+1,y+1);
        }
        void move(guint x,guint y) {
            board.remove(this);
            this.x = x;
            this.y = y;
            board.attach(this,x,y,x+1,y+1);
        }

    private:
        guint x, y;
        board board;
        GtkImage image;

};

class board: Gtk::Table {

    public:
        board(): Gtk::Table(25,20) {
            blocks_c = 0;
        }
        void addBlock(guint x,guint y) {
            blocks_a[blocks_c++] = new block(this,x,y);
        }

    private:
        block* blocks_a[24];
        int blocks_c;

};
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,"块"类需要知道"板"是什么,反之亦然.提前致谢!

c++ circular-dependency forward-declaration

1
推荐指数
2
解决办法
3152
查看次数

为什么不能在外部名称空间中定义完全限定的函数?我可以知道标准说的是什么吗?

这是错的吗?为什么?我可以知道标准说的是什么吗?

 namespace N{
  namespace N1{
     namespace N2{
        struct A{
         struct B{
          void fun();
       };//B
     }; //A
   } //n2
 }//n1
 namespace N3{
     void N1::N2::A::B::fun(){} //error
 }//n3
}//n

int main()
 {
  return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我可以知道为什么会失败吗?

c++ namespaces

0
推荐指数
1
解决办法
120
查看次数

使用STL堆栈作为C++类的成员

我正在开发一个需要列表(单独)堆栈的项目.我创建了一个名为"stacklist"的类,就是堆栈列表.我希望类有一个指向列表头部的堆栈指针.但是,每当我尝试构建类时,我都会收到错误.

#ifndef STACKLIST_H
#define STACKLIST_H

#include <stack>

//source: Data Structures Using C++: Linked List Implementation Part II (List Class)
//url: http://www.youtube.com/watch?feature=endscreen&NR=1&v=7vZo17iv1zQ
class stacklist
{
    public:
        stacklist();
        void addToHead(const int&);
        void printList();
        void insert(const int&);

    private:
        stack<int>* head; //THIS IS WHERE I AM HAVING THE PROBLEM
        int size;

};



#endif // STACKLIST_H
Run Code Online (Sandbox Code Playgroud)

错误消息是"error:expected';' 在'<'标记之前

任何可以提供的见解都将受到高度赞赏.

c++ stack stl class

0
推荐指数
2
解决办法
372
查看次数