小编yau*_*ser的帖子

用C或C++实现Prolog

我想知道Prolog在C或C++中的实现方式如何.我主要感兴趣的是将它构建为C或C++库,尽管解释器应用程序也可以.我有兴趣阅读它的内部,即查询执行,即查找解决方案和相关的数据类型.如果您向我推荐任何有关主题的阅读材料或任何直接建议/建议,我将很高兴.读数可能适用于其他OOP语言或一般OOP.最耗尽的材料将解决这个问题.

c++ prolog

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

Makefile在多个目录中

我想构建一个应用程序,我有多个模块存储在多个目录中.我决定遵循这个想法,即在每个目录中都有一个makefile然后合并它.但是 - 作为初学者程序员 - 我仍然没有看到如何做到这一点.首先,这种"部分"的makefile是怎样的.它们不能具有main函数,因为每个二进制文件只能有一个,但是当我尝试编译它时,gcc会抱怨对main的未定义引用.其次,我不知道如何将所有这些模块组合在一起.

我将不胜感激,但请尽量保持简单的答案.Makefile对我来说仍然是一个黑魔法.

makefile

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

在纯C中打开Unicode文件

我正在尝试打开一个全中文的.txt文件。即使数据流为100%Unicode,我是否也可以对其使用常规的fopen / fclose过程?是否有用于处理宽字符的排他性工具?我将为您提供准确的答案,我是一名初学者。我正在使用带有标准gcc的Linux。

我将附加我的代码,它编译时没有错误,但是在执行时会出现分段错误。我不知道这是怎么回事。该程序的重​​点是复制每个字符串,从中找到给定集合中的特定符号,并将其写入单独的文件中。

#include<stdio.h>
#include<stdlib.h>
#include<wchar.h>
#include <locale.h>
#define PLIK_IN in /*filenames*/
#define PLIK_OUT out
#define LKON 49 /*specifying the length of a string on the left from a desired sign*/
#define PKON 50 /*...and on the right*/
int wczytaj_pliki(FILE*, FILE*); /*open file*/
void krocz_po_pliku(FILE*, FILE*); /*search through file*/
int slownik(wchar_t); /*compare signs*/
void zapisz_pliki(FILE*, FILE*); /*write to file*/

void main(void)
{
    FILE *bin,*bout;
    setlocale(LC_CTYPE, "");

    wczytaj_pliki(bin, bout);
    krocz_po_pliku(bin, bout);
    zapisz_pliki(bin, bout);
}/*main*/

int slownik(wchar_t znak) /*compare characters*/
{ …
Run Code Online (Sandbox Code Playgroud)

c unicode fopen wchar-t cjk

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

ISO C++禁止在指向函数的指针和指向对象的指针之间进行转换

我希望有一个类能够保持其字段指向函数的指针和指向结构保持的指针是参数.该对象的接口是一个方法call(),它不带参数,但将保存的参数传递给上面提到的函数.针对不同参数类型和计数的这类类的一族将具有共同的抽象祖先,其中调用是虚拟的.

至于现在我有以下代码可行,虽然向g ++添加-pedantic选项会产生错误:

class Function {
    protected:
    void *data;
    void *function;
    public:
    virtual void call() = 0;
};

class SingleArgumentFunction : public Function {
    public:
    SingleArgumentFunction( void (*f)(int), int i ) {
        int *icpy = new int(i);
        function = (void*) f;
        data = (void*) icpy;
    }
    ~SingleArgumentFunction() { delete (int*)data; }
    inline void call() {
        ( *((void (*)(int))function) )( *(int*)data );
    }
};
Run Code Online (Sandbox Code Playgroud)

我得到的错误是标题所示:

warning: ISO C++ forbids casting between pointer-to-function and pointer-to-object
Run Code Online (Sandbox Code Playgroud)

怎么处理?

c++ function-pointers

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

函数模板的隐式实例化

我有以下代码,我只是为了练习功能模板而构思的.

#include <iostream>

template <typename T>
T fun( const T &t ) { return t; }

struct A {
    int dataf;
    A( int a ) : dataf(a) { std::cout << "birth\n"; }
    friend A fun( const A & );
};

int main(){
    A a( 5 );
    fun( a );   
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

虽然我收到以下错误:

code.cc:(.text+0x32): undefined reference to `fun(A const&)'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我理解好类模板,但我仍然对功能模板感到困惑.

c++ templates

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

没有已知的从指针转换为指针的转换

我有以下g ++错误

Menu.hpp:66:41: error: no matching function for call to ‘Menu::Stack<Submenu*>::push(Submenu*)’
Menu.hpp:66:41: note: candidate is:
Menu.hpp:14:21: note: void Menu::Stack<T>::push(T&) [with T = Submenu*]
Menu.hpp:14:21: note:   no known conversion for argument 1 from ‘Submenu*’ to ‘Submenu*&’
Run Code Online (Sandbox Code Playgroud)

怎么这样的转换是不可能的?编译器在什么场合发出这样的错误?

至于我在做什么:

  • 我有一个类Submenu
  • 我有一个继承自子菜单的类菜单
  • 菜单具有附加的字段类型Stack<Submenu*>,用于映射记住打开的子菜单
  • 菜单的所有方法,如"打开菜单","单击菜单项"等,都是指当前位于堆栈顶部()的子菜单.在类Submenu中,它们对对象本身进行操作.
  • 菜单有一个关闭当前子菜单并上升的公共方法 - 即从堆栈中弹出子菜单.
  • 菜单可以弹出,直到它到达自己,看下面是什么意思.

现在是最有可能成为问题的部分,即Menu的构造函数:

Menu() { stack.push( (Submenu*)this ); }
Run Code Online (Sandbox Code Playgroud)

它这样做是因为当所有菜单都关闭时,相对于stack.top()的方法应该引用Menu本身,也是一种Submenu(因为它继承了它).

编辑:

我已经创建了自己的类Stack而不是使用std :: stack(正如我最初建议的那样),正如答案中所指出的那样,它存在问题.请原谅我的不准确之处.

c++ inheritance constructor

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

不完整类型的无效使用(嵌套类情况)

我怎样才能在 C++ 中实现这样的想法而不陷入“无效使用不完整类型”的麻烦?

class A {
    /*(...) some fields and methods here. */
    class B {
        /*(...) some fields and methods here. */
        friend B A::fun();
    };
    B fun();
};
Run Code Online (Sandbox Code Playgroud)

c++ forward-declaration incomplete-type

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

是否允许在对象外定义辅助函数?

我有一个类和一个给定的非常复杂的方法,为此我已经定义了一些辅助函数,但是在其他任何地方都没有使用它们.现在我想知道我是否应该这样做

  • 将它们添加到我的班级 - 它似乎是根据OOP范例应该做的事情
  • 将它们保留在外部,即仅在我的实现文件中 - 因为用这种半冗余方法填充我的类定义会使我的类定义更不易读.

我的问题是当涉及C++风格时我该怎么办.我觉得第二种解决方案违背了OOP原则,尽管C++不是面向对象的语言,而是混合语言.我提到的函数,如果实现为类方法,将是静态的.

c++ coding-style

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

三元运算符使用时的奇怪错误

我有以下代码:

#include <iostream>
int main() {
    int i = 3;
    do {
        (i == 3) ? (std::cout << "Is 3.\n") : ++i;
        ++i;

    } while ( i < 4 );
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

并在响应中收到以下错误:

ternary.cc: In function ‘int main()’:
ternary.cc:5:43: error: invalid conversion from ‘void*’ to ‘int’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)

我的代码出了什么问题?

c++

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

使用模板时奇怪的"非const引用的无效初始化"

我收到以下错误:

Database.hpp: In member function ‘Table<T, S>& Table<T, S>::operator=(const Table<T, S>&) [with T = int, int S = 3, Table<T, S> = Table<int, 3>]’:
Database.hpp:40:8:   instantiated from ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = List, _Alloc = std::allocator<List>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<List*, std::vector<List> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = List*]’
/usr/include/c++/4.6/bits/stl_vector.h:834:4:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = List, _Alloc = std::allocator<List>, std::vector<_Tp, _Alloc>::value_type = List]’
SelectiveReading.hpp:139:34:   instantiated from here
Database.hpp:34:16: error: invalid initialization of non-const reference …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors

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