假设我有两个相互引用的数据结构。我想将它们放入单独的头文件中,如下所示:
\n // datastruct1.h\n #ifndef DATA_STRUCT_ONE\n #define DATA_STRUCT_ONE\n\n #include <datastruct2.h>\n typedef struct DataStructOne_t\n {\n DataStructTwo* two;\n } DataStructOne;\n #endif\n
Run Code Online (Sandbox Code Playgroud)\n和
\n // datastruct2.h\n #ifndef DATA_STRUCT_TWO\n #define DATA_STRUCT_TWO\n\n #include <datastruct1.h>\n typedef struct DataStructTwo_t\n {\n DataStructOne* one;\n } DataStructTwo;\n\n #endif\n
Run Code Online (Sandbox Code Playgroud)\n我有一个main
功能:
#include <datastruct1.h>\n #include <datastruct2.h>\n\n int main() \n {\n DataStructOne* one;\n DataStructTwo* two;\n }\n
Run Code Online (Sandbox Code Playgroud)\n然而我的编译器抱怨:
\n$ gcc -I. -c main.c\nIn file included from ./datastruct1.h:4,\n from main.c:1:\n./datastruct2.h:8:2: error: unknown type name \xe2\x80\x98DataStructOne\xe2\x80\x99\n 8 | DataStructOne* one;\n …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个像客户端一样的短程序,如telnet.我收到的用户输入如下:www.google.com 80(端口号)和/index.html但是,我收到了一些错误.当我写一些调试信息时,它说我有一个错误的文件描述符和文件描述符100上的读取失败messagesize = 0.
struct hostent * pHostInfo;
struct sockaddr_in Address;
long nHostAddress;
char pBuffer[BUFFER_SIZE];
unsigned nReadAmount;
int nHostPort = atoi(port);
vector<char *> headerLines;
char buffer[MAX_MSG_SZ];
char contentType[MAX_MSG_SZ];
int hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hSocket == SOCKET_ERROR)
{
cout << "\nCould Not Make a Socket!\n" << endl;
return 1;
}
pHostInfo = gethostbyname(strHostName);
memcpy(&nHostAddress,pHostInfo -> h_addr, pHostInfo -> h_length);
Address.sin_addr.s_addr = nHostAddress;
Address.sin_port=htons(nHostPort);
Address.sin_family = AF_INET;
if (connect(hSocket, (struct sockaddr *)&Address, sizeof(Address)) == SOCKET_ERROR)
{
cout << "Could not …
Run Code Online (Sandbox Code Playgroud) 所有,我试图了解我们究竟是如何计算两个数组之间的反转次数.
比方说,我们有以下两个数组:
A = [1,2,3,4,5,6] B = [6,3,5,4,2,1]
我如何从概念上计算反演次数?也就是说,只看这两个数组,忽略所涉及的编码.
另外,我知道在两个数组之间绘制线段的惯例,但我试图在这里获得更深入的理解.
谢谢!
我正在尝试学习新的c ++标准,但是在使用智能指针时,我的工作时间很短.这是我正在编写的程序的代码,它不想工作:
#include <iostream>
#include <memory>
template <typename T> class printer
{
public:
printer(T val)
{
value = val;
}
void printvalue()
{
std::cout << "The value is " << val;
std::cin.get();
}
private:
T value;
};
template <typename T> class test
{
public:
test(T value)
{
printer<T> * test = new printer<T>(value);
*printValue = test;
}
void beginTest()
{
printValue.get()->printvalue();
}
private:
std::unique_ptr<printer<T>> printValue;
};
Run Code Online (Sandbox Code Playgroud)
我写了这样的主函数:
int main()
{
test<int> t(5);
t.beginTest();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我压缩之后,我得到以下错误:binary'=':找不到运算符,它采用'printer*'类型的右手操作数(或者没有可接受的转换)
这是指这行代码:*printValue = test; …
我正在创建一个程序,它仅使用 1、2、6 和 13 返回获得数字 (n) 所需的最少总和。它非常适合 n 的小值,但一旦 n 达到像 200 这样的值程序花费太多时间来计算结果。
因此,我有两个问题:
1.有没有办法让递归更快?
2.我应该避免使用递归并使用循环吗?
这是注释的代码:
#include <iostream>
#define MAX 500000
using namespace std;
void cal(int inp, int &mini, int counter = 0);
int main (void)
{
//Gets input
int n;
cin >> n;
//Defines mini as the MAX result we can get
int mini = MAX;
//Calls the function
cal(n, mini);
//Prints the best result
cout << mini << endl;
return 0;
}
void cal(int inp, …
Run Code Online (Sandbox Code Playgroud) 我想知道我是否可以制作适用于手机和平板电脑的通用应用.现在我开发了一个带按钮的移动应用程序.但是当我去平板电脑时,按钮大小保持不变.我想在平板电脑上增加按钮大小.
什么是指针?什么是解除引用?如果p
是指针,*p = some_value
和之间有什么区别p = other_value
?什么p = &some_variable
意思?什么是NULL指针?取消引用NULL指针时会发生什么?
我有一些类和它们的实例。该示例显示了一些无意义的类。它们的确切性质并不重要。
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
class Foo a where
foo :: a -> Int
class Bar a where
bar :: a -> String
instance Foo Int where
foo x = x
instance Foo String where
foo x = length x
instance Bar Int where
bar x = show x
instance Bar String where
bar x = x
Run Code Online (Sandbox Code Playgroud)
好的,现在我想创建一些存在类型,将这些类隐藏在某些数据类型外观后面,这样我就不必处理约束了。(我知道存在类型被认为是一种反模式,请不要向我解释这一点)。
data TFoo = forall a. Foo a => TFoo a …
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
for (auto e : foo | ::std::views::take_while([](const char* x) { return x != nullptr; }))
std::cout << e << "\n";
Run Code Online (Sandbox Code Playgroud)
一切都很好,很花哨,但是这个 lambda 很长很笨拙。由于它发生了很多次,我决定用一个命名的函数对象来代替。
class non_null
{
template <typename T> bool operator()(const T* t) {
return t != nullptr;
}
};
for (auto e : foo | ::std::views::take_while(non_null()))
std::cout << e << "\n";
Run Code Online (Sandbox Code Playgroud)
这无法通过大量错误消息进行编译,顶部有“类模板参数推导失败”。
为什么?可以做些什么呢?(除了take_while
使用非模板化谓词调用,这显然有效)。
(这里是生锈菜鸟;我试图了解在高阶函数情况下可以/不能/应该/不应该通过引用传递什么)
let a = [1, 2, 3];
Run Code Online (Sandbox Code Playgroud)
此调用有效:
let sum = a.iter().fold(0, |acc: i32, x: &i32| acc + x);
Run Code Online (Sandbox Code Playgroud)
这些不:
let sum = a.iter().fold(0, |acc: i32, x: i32| acc + x);
let sum = a.iter().fold(0, |acc: &i32, x: i32| acc + x);
Run Code Online (Sandbox Code Playgroud)
错误信息是
error[E0631]: type mismatch in closure arguments
--> main.rs:8:22
|
8 | let sum = a.iter().fold(0, |acc: &i32, x: i32| acc + x);
| ^^^^ --------------------------- found signature of `for<'r> fn(&'r i32, i32) -> _`
| |
| …
Run Code Online (Sandbox Code Playgroud)