我想知道Prolog在C或C++中的实现方式如何.我主要感兴趣的是将它构建为C或C++库,尽管解释器应用程序也可以.我有兴趣阅读它的内部,即查询执行,即查找解决方案和相关的数据类型.如果您向我推荐任何有关主题的阅读材料或任何直接建议/建议,我将很高兴.读数可能适用于其他OOP语言或一般OOP.最耗尽的材料将解决这个问题.
我想构建一个应用程序,我有多个模块存储在多个目录中.我决定遵循这个想法,即在每个目录中都有一个makefile然后合并它.但是 - 作为初学者程序员 - 我仍然没有看到如何做到这一点.首先,这种"部分"的makefile是怎样的.它们不能具有main函数,因为每个二进制文件只能有一个,但是当我尝试编译它时,gcc会抱怨对main的未定义引用.其次,我不知道如何将所有这些模块组合在一起.
我将不胜感激,但请尽量保持简单的答案.Makefile对我来说仍然是一个黑魔法.
我正在尝试打开一个全中文的.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) 我希望有一个类能够保持其字段指向函数的指针和指向结构保持的指针是参数.该对象的接口是一个方法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)
怎么处理?
我有以下代码,我只是为了练习功能模板而构思的.
#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)
我理解好类模板,但我仍然对功能模板感到困惑.
我有以下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)
怎么这样的转换是不可能的?编译器在什么场合发出这样的错误?
至于我在做什么:
Stack<Submenu*>,用于映射记住打开的子菜单现在是最有可能成为问题的部分,即Menu的构造函数:
Menu() { stack.push( (Submenu*)this ); }
Run Code Online (Sandbox Code Playgroud)
它这样做是因为当所有菜单都关闭时,相对于stack.top()的方法应该引用Menu本身,也是一种Submenu(因为它继承了它).
我已经创建了自己的类Stack而不是使用std :: stack(正如我最初建议的那样),正如答案中所指出的那样,它存在问题.请原谅我的不准确之处.
我怎样才能在 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++风格时我该怎么办.我觉得第二种解决方案违背了OOP原则,尽管C++不是面向对象的语言,而是混合语言.我提到的函数,如果实现为类方法,将是静态的.
我有以下代码:
#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)
我的代码出了什么问题?
我收到以下错误:
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++ ×8
c ×1
cjk ×1
coding-style ×1
constructor ×1
fopen ×1
inheritance ×1
makefile ×1
prolog ×1
templates ×1
unicode ×1
wchar-t ×1