小编Kal*_*lec的帖子

PTHREAD_MUTEX_INITIALIZER与pthread_mutex_init(&mutex,param)

两者之间有什么区别吗?

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
Run Code Online (Sandbox Code Playgroud)

要么

pthread_mutex_t lock;
pthread_mutex_init ( &lock, NULL);
Run Code Online (Sandbox Code Playgroud)

如果我只使用第一种方法,我是否足够安全?

注意:我的问题主要是指非常小的程序,我最多会将几个客户端连接到服务器并使用工作线程解决它们的查询.

c ubuntu mutex pthreads

82
推荐指数
4
解决办法
7万
查看次数

套接字错误:连接被拒绝 - 我做错了什么?

我刚开始学习套接字的基础知识(Linux).我尝试了一个小例子,但它没有用,我不知道出了什么问题.

我收到"拒绝连接"错误消息.


这是我的代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main() {
    int c;
    c = socket(AF_INET, SOCK_STREAM, 0);
    if (c < 0) {
        printf("Error in creating socket! %s\n", strerror(errno));
        return 1;
    }

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_port = htons(1234);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr("127.0.0.1"); //local host
    if (connect(c, (struct sockaddr *)&server, sizeof(server)) < 0) {
        // Here is my error
        printf("Error when connecting! %s\n",strerror(errno)); 
        return 1;
    }

    while(1) …
Run Code Online (Sandbox Code Playgroud)

c sockets linux

11
推荐指数
2
解决办法
3万
查看次数

从"并行"子目录导入另一个目录中的模块

我希望有一个看起来像这样的层次结构(它必须看起来像这样)

main_folder\
    main.py
    domain_sub_directory\
        __init__.py
        domain.py
    ui_sub_direcotory\
        __init__.py
        menu.py
Run Code Online (Sandbox Code Playgroud)

我需要激活ui.py frome main.py,然后从menu.py访问domain.py. 我怎样才能做到这一点 ?

我主要做了这个:

    import ui_sub_directory.ui
Run Code Online (Sandbox Code Playgroud)

这在ui中:

    import domain_sub_directory.domain
Run Code Online (Sandbox Code Playgroud)

但是UI模块没有看到域模块.

我究竟做错了什么 ?

BTW我还需要导入我正在使用的课程吗?这和之间有什么区别:

from x import y 
Run Code Online (Sandbox Code Playgroud)

*编辑*为那些不理解我想从中导入的人:

folder1 /folder2 /folder3 /module1 
Run Code Online (Sandbox Code Playgroud)

我想导入这个:

folder1 /folder2 /module2
Run Code Online (Sandbox Code Playgroud)

python

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

在构造函数外部使用.setVisible()会破坏我的GUI

我刚刚学习java GUI的基础知识.我有这种奇怪的情况,我无法解释.

我有一个GUI类,我在其中构建一个简单的JFrame.如果我.setVisible(true) 在构造函数中使用一切正常,如果我在外面使用它,没有任何加载(窗口是可见的,但按钮和什么不是).

为什么会这样?

public class GUI extends JFrame {


    private JTextField humanYears_TextField = new JTextField(3);
    private JTextField dogYears_TextField = new JTextField(3);
    private JButton convert_Button = new JButton("Convert");
    private JLabel greeting = new JLabel ("Dog years to Human years!");

    public GUI () {

        JFrame window = new JFrame();
        JPanel content = new JPanel();


        content.setLayout(new FlowLayout());
        content.add(this.greeting);
        content.add(new JLabel("Dog Years: "));
        content.add(this.dogYears_TextField);
        content.add(this.convert_Button);
        content.add(new JLabel("Human Years: "));
        content.add(this.humanYears_TextField);

        window.setContentPane(content);
        pack(); // aplica contentPane-ul
        window.setLocationRelativeTo(null); …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing multiple-instances jframe

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

如何验证string.split()是否返回null

我正在从文件中读取数据:

Some Name;1|IN03PLF;IN02SDI;IN03MAP;IN02SDA;IN01ARC
Some Other Name;2|IN01ALG;IN01ANZ
Another Name;3|
Test Testson;4|IN03MAP;IN01ARC;IN01ALG
Run Code Online (Sandbox Code Playgroud)

我对从该文件中读取的每一行使用string.split(),如下所示:

String args[]         = line.split("\\|");                  
String candidatArgs[] = args[0].split(";");

if (args[1] != "" || args[1] != null) { 

    String inscrieriString[] = args[1].split(";");
Run Code Online (Sandbox Code Playgroud)

事情是:当我到达Another Name;3|之后.split("\\|")的第二部分(args[1])应该是空的,要么null"" (我真的不知道).

但是我得到一个数组索引上界失误if (args[1] != "" || args[1] != null)(再次,网址为:Another Name;3|)

java string-split

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

抛出主要异常和子类型,是否有正确的方法?

好的,所以我在SO和程序员之间有一堆与异常相关的问题,但是有很多要问的问题,或者我不知道要输入什么,或者不是很多人问的.

所以,假设我有一个抛出FileNotFoundException(FNFE)的方法.然后我有另一个方法使用第一个,但也抛出一个IOException(IOE).

我的处理程序会同时捕获它们并对每个执行不同的操作,但是我的IDE(IntelliJ)发出信号,我已经在"抛出列表中有一个更普遍的例外,'java.io.IOException'".

我知道如果我这样做会有效:

public File openOrCreate(String pathStr) throws FileNotFoundException,
                                                IOException {
    try {

        // Method that generates the FNFE
        Path path = ReposioryProposition.getPath(pathStr);
        File file = path.toFile();

    catch (FileNotFoundException fnfe) {
        throw fnfe;
    }

    if (!file.exists())
        file.createNewFile();  // IOE
    return file;

}
Run Code Online (Sandbox Code Playgroud)

但我需要明确地做吗?它会没有工作,或者更危险的是,将有时没有明确的版本.

为了确保我们在同一页面,这是我最初编写的东西:

public File openOrCreate(String pathStr) throws FileNotFoundException,
                                                IOException {

    Path path = ReposioryProposition.getPath(pathStr);
    File file = path.toFile();

    if (!file.exists())
        file.createNewFile();
    return file;

}
Run Code Online (Sandbox Code Playgroud)

但我不确定会发生什么,是FNFE被抛出还是被吞没了?我的目的是分别抓住它们并为另一个做不同的事情.

java exception-handling

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

异常以简单的方式处理语法问题

注意:我不能使用任何默认值.

我试图做一个非常简单的异常处理例程,或者至少做一些看起来很重要的事情.我不想做太多,只是抛出异常并打印错误消息.

在.h

class MyException {
    protected: string message;

    public:

        MyException (string mes) {
            this->message = mes;
        }

        MyException (); // is this necessary ? does it do anything ?

        string getMessage() const {
            return this->message;
        }
};
Run Code Online (Sandbox Code Playgroud)

我想要的是拥有"PersonException"和"ActivityException".可能会使用模板但不确定是否可以解决问题.

class PersonException:public MyException {

    public:

        PersonException (string message):MyException(message) {

        }
};


class PersonValidator {

    public:

        PersonValidator (Person p) throw (PersonException);
};
Run Code Online (Sandbox Code Playgroud)

在.cpp

void PersonValidator::PersonValidator(Person p) throw (PersonException) {
    if (p.getPhone < 0) {
        throw PersonException ("Person Number is invalid");
}
Run Code Online (Sandbox Code Playgroud)

这有什么不对或麻烦,怎么可能做得更好?我在哪里打印错误信息?

c++ exception-handling

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

不知道如何使用模板调用类函数

我正在使用模板创建自己的字典(不,我不能,我不会使用STL中的任何内容).

我想要一个非常简单的搜索功能,但我有一个小问题.

template <typename TElement>
void Dictionary<TElement>::search(TElement ADT, int key) {  // Abstract Data Type
    inf flag = 0;
    index =  int (key % max);
    temp[index] = root[index]; // root of the hash
    while (temp[index]->next != NULL) {
        if(temp[index]->data->key_actual_name == key) { @things happen }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要了解的内容:如何使用模板,以便我可以拥有,temp[index]->data-><template call>如果这是有道理的

我想通过使用来调用字典:Class_type == TElement和"key"总是一个int但它可以是不同的东西.它可能是ID或电话号码.问题是我需要使用密钥的实际名称(if(temp[index]->data->ID (or phone or what ever) == key){@things happen}),我想我可以在这里使用模板,但我不知道如何.

也许相关:

template <typename TElement>
typedef struct list{
    TElement data;
    struct list *next;
}node_type; …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

在Java中使用泛型类和泛型方法很难

java的新手,我确实搜索了一些例子,但我真的很难过.

我有一个使用泛型的节点类:

public class Node<T> {

    private int key;
    private T data;
    private Node<T> nextNode;

}
Run Code Online (Sandbox Code Playgroud)

节点是好的.

public class GenericDictionary implements GenericDictionary_interface {

    private int capacity;
    private Node<T> [] slots;

    public void add (Node<T> newNode) {

    }
}
Run Code Online (Sandbox Code Playgroud)

我想这就是我写它的方式.我希望GenericDictionary与节点对象一起使用.

我收到一个错误:"T无法解析为类型".

事情是我不知道它应该是什么样子.

java generics

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

扫描仪只读取文件名而不读取其他内容

我正在努力实现一个基本的词法分析器.我现在仍然坚持解析文件.

public ArrayList<Token> ParseFile () {

    int lineIndex = 0;
    Scanner scanner = new Scanner(this.fileName);

    while (scanner.hasNextLine()) {

        lineIndex++;
        String line = scanner.nextLine();

        if (line.equals(""))
        continue;

        String[] split = line.split("\\s"); 
        for (String s : split) {
        if (s.equals("") || s.equals("\\s*") || s.equals("\t"))
        continue;
        Token token = new Token(s, lineIndex);
        parsedFile.add(token);

        }
    }
    scanner.close();
    return this.parsedFile;
}
Run Code Online (Sandbox Code Playgroud)

这是我的名字叫"p ++.ppp"

#include<iostream>

using namespace std ;

int a ;
int b ;

int main ( ) {

    cin >> a ;
    cin >> …
Run Code Online (Sandbox Code Playgroud)

java parsing java.util.scanner

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