小编yak*_*yak的帖子

双链表模板复制构造函数赋值运算符

我在C++中使用模板编写了一个双向链表的简单实现.但是,我在复制构造函数和赋值运算符方面遇到了一些问题.我的代码给了我分段错误,我不知道如何修复它以及错误在哪里(问题在于复制构造函数和赋值运算符):

#include <iostream>
using namespace std;

template <class T>
class MyNode
{
public:
    MyNode(const T &e = T(), MyNode *n = NULL, MyNode *p = NULL) : element(e), next(n), previous(p) {}
    ~MyNode() {}
    T element;
    MyNode *next;
    MyNode *previous;

};

template <class T>
class MyList
{
private:
    MyNode<T> *head;
    MyNode<T> *tail;

public:
    MyList()
    {
        head = new MyNode<T>();
        tail = new MyNode<T>();
    }
    ~MyList()
    {
        clear();
        delete head;
        delete tail;
    }
    MyList(const MyList& otherList)
    {
        while (otherList.head->next!=NULL)
        {
            MyNode<T> *curr …
Run Code Online (Sandbox Code Playgroud)

c++ templates linked-list doubly-linked-list

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

CORBA C++/Java应用程序中服务器端的分段错误(核心转储)

我有这样的代码:

interface Employee
{
    string getLastname();
};

#include "Employee.idl"

interface Work
{
    Employee getEmployee(in short id);
};
Run Code Online (Sandbox Code Playgroud)

服务器文件:

#include "Employee.hh"

class EmployeeImpl : public POA_Employee
{
    private:
        char* lastname;
        int id;

    public:
        EmployeeImpl(const char* lastname, int id);
        char* getLastname();
};

#include "EmployeeImpl.h"

EmployeeImpl::EmployeeImpl(const char* lastname, int id)
{
    this->lastname = const_cast<char*>(lastname);
    this->id = id;
}

char* EmployeeImpl::getLastname()
{
    return this->lastname;
}

#include "Work.hh"
#include <vector>
#include "EmployeeImpl.h"
using namespace std;

class WorkImpl : public POA_Work
{
    private:
        vector<EmployeeImpl> employees;

    public: …
Run Code Online (Sandbox Code Playgroud)

c++ corba segmentation-fault omniorb

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

获取当前时间C,功能

我想在C中获取当前时间(没有当前日期).主要问题是当我想用函数做的时候.当我不使用它们时,evertyhing就好了.任何人都可以告诉我,为什么我的代码只显示一个小时?(看一下附图).提前致谢.

#include <stdio.h>
#include <time.h>
#include <string.h>

char* get_time_string()
{
    struct tm *tm;
    time_t t;
    char *str_time = (char *) malloc(100*sizeof(char));
    t = time(NULL);
    tm = localtime(&t);
    strftime(str_time, sizeof(str_time), "%H:%M:%S", tm);
    return str_time;
}

int main(int argc, char **argv)
{
    char *t = get_time_string();
    printf("%s\n", t);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

c time

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

Qt Linguist - 为应用程序Qt*.ui文件设置翻译器

我在菜单中选择了一种语言后,编写了一个简单的小例子来更改应用程序语言.虽然连接DOES工作(qDebug()打印好消息)但它不会改变我的QLabel上的文本.我使用QtDesigner创建了GUI.注意:所有这些文件都在同一目录中.我正在使用Qt5.继承我的代码:

*.pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = qt_pl_en
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

TRANSLATIONS += ic.ts
TRANSLATIONS += se.ts
Run Code Online (Sandbox Code Playgroud)

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTranslator>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    retranslate();

    QObject::connect(ui->action_Icelandic, SIGNAL(triggered()), this, SLOT(speakIcelandic()));
    QObject::connect(ui->action_Swedish, SIGNAL(triggered()), this, SLOT(speakSwedish()));
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::changeEvent(QEvent* event)
{
    if (event->type() == QEvent::LanguageChange)
    {
        // retranslate designer …
Run Code Online (Sandbox Code Playgroud)

qt translation linguistics

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

Java HashMap <String,ArrayList <Object [] >>如何添加空数组?

我在我的应用程序中使用HashMap,有时我需要添加一个带有空值的键(String)(对象的emty数组列表).但Netbeans 7.4说:

Exception in thread "main" java.lang.NullPointerException
    at test.Version.main(Version.java:35)
Java Result: 1
Run Code Online (Sandbox Code Playgroud)

这段代码:

        HashMap<String, ArrayList<Object[]>> d = null;

        ArrayList<Object[]> a;
        a = new ArrayList<>();

        d.put("key1", a);
Run Code Online (Sandbox Code Playgroud)

我不想使用MultiMap.有没有其他方法可以轻松解决?

java arraylist hashmap

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

如何比较双数?

我知道当我想检查双倍==双时我应该写:

bool AreSame(double a, double b)
{
    return fabs(a - b) < EPSILON;
}
Run Code Online (Sandbox Code Playgroud)

但是当我想检查是否a > b或b > a?

c floating-point double comparison double-precision

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

在wx.Notebook上的Panel上展开wx.GridBagSizer的子元素

我有一个带有 wx.Notebook 的简单应用程序,它看起来像这样:

在此输入图像描述

为了布局它的子元素,我使用了 wx.GridBagSizer - 它工作得很好。然而,我遇到了一个小但烦人的问题。当我展开 wx.Notebook 的窗口时,它不会展开所有元素并导致窗口如下所示:

在此输入图像描述

这实际上不是我想要的。我应该如何修改代码以便在调整窗口大小时能够展开所有子元素?干杯:)

我的代码很简单:

import wx
import wx.richtext

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='wx.Notebook')
        book = wx.Notebook(self)
        page = MyPage(parent=book)
        book.AddPage(page, 'Page 1')
        book.AddPage(MyPage(book),'Page 2')
        book.AddPage(MyPage(book),'Page 3')
        self.SetSize(wx.Size(320,250))
        self.Centre()

class MyPage(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, size=(0,0))
        # create sizer to add a border

        bagSizer = wx.GridBagSizer(15, 10)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        label = wx.StaticText(self, label="Text:")

        self.content1 = wx.TextCtrl(self, size=(300, 20))
        self.content2 = wx.richtext.RichTextCtrl(self, size=(300, 100), style=wx.TE_MULTILINE)

        bagSizer.Add(self.content1, pos=(0, 0), flag=wx.TOP | wx.LEFT …
Run Code Online (Sandbox Code Playgroud)

python layout wxwidgets wxpython

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

如何编写以给定数字开头的正则表达式?

我想写一个程序来检查一个以给定数字开头的字符串.让我们说,我们有一个字符串:line = "5p.m. its a lovely time for tea!"然后我从用户读取一个数字:num = raw_input()并想检查我的行是否以用户通过的数字开头.

我想做点什么(这可能是一些错误):

s = compile(r"^[\<%\>]", num)
m = s.search(line)
Run Code Online (Sandbox Code Playgroud)

要么

s = compile(r"^[\<%\>]", num)
m = s.search(line)
Run Code Online (Sandbox Code Playgroud)

当用户传递5时,当行以5开头时,它会打印"OK"或类似的东西,或者当我的行以不同的数字开头时,它会打印"It not OK".我是这个整个正则表达式的新手,请帮助:)我接受C++和Python的代码;)

python regex

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

不要修改程序,打印一些值作为输出 - 异常(?)作业

对我来说,我有一个不寻常的功课问题:

不要修改主函数,但更改它作为输出打印的程序:

  • 星期一
  • 是第一天
  • 一周中的

并且继承了给定的代码:

int main()
{
  cout << "is the first day\n";
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激 :)

c++

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

为什么来自stdlib的qsort无法使用双精度值?[C]

我写了一个简单的程序来整理数组。问题在于,仅当我需要数组具有double元素时,代码才可以使用int值...有帮助吗?

#include <stdio.h>
#include <stdlib.h>

double values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}

int main()
{
    int n;

    printf("Before sorting the list is: \n");
    for( n = 0 ; n < 5; n++ )
    {
        printf("%.2f ", values[n]);
    }

    printf("\n\n");

    qsort(values, 5, sizeof(double), cmpfunc);

    printf("\nAfter sorting the list is: \n");
    for( n = 0 ; n < 5; …
Run Code Online (Sandbox Code Playgroud)

c double qsort

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

迭代日期范围Java,奇怪的结果

我想在不使用库(即Joda)的情况下迭代日期范围.我写了那个简单的代码,但结果很奇怪.例如,对于日期2010-01-01 2010-02-01,它打印:

1.0.2010
2.0.2010
3.0.2010
4.0.2010
5.0.2010
6.0.2010
7.0.2010
8.0.2010
9.0.2010
10.0.2010
11.0.2010
12.0.2010
13.0.2010
14.0.2010
15.0.2010
16.0.2010
17.0.2010
18.0.2010
19.0.2010
20.0.2010
21.0.2010
22.0.2010
23.0.2010
24.0.2010
25.0.2010
26.0.2010
27.0.2010
28.0.2010
29.0.2010
30.0.2010
31.0.2010
1.1.2010
Run Code Online (Sandbox Code Playgroud)

日期也存在问题:2010-05-01 2010-06-01(打印4月而不是5月).有帮助吗?

import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.text.DateFormatSymbols;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import javax.swing.JOptionPane;
import java.util.GregorianCalendar;

class test
{
    public static void main(String[] args) {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null, endDate = null;

        try …
Run Code Online (Sandbox Code Playgroud)

java date

-1
推荐指数
1
解决办法
4699
查看次数