struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Run Code Online (Sandbox Code Playgroud)
最后的“书”到底是什么?
在 C++ 中,假设我需要为变量分配一些东西,我想在 main() 之外做这件事,所以代码更清晰,但是我想在 main() 或另一个函数中使用该变量进行某些操作。例如我有:
int main()
{
int a = 10;
int b = 20;
SomeFunction(a,b);
}
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
void Init()
{
int a = 10;
int b = 20;
}
int main()
{
SomeFunction(a,b);
}
Run Code Online (Sandbox Code Playgroud)
但显然编译器会说 a 和 b 在 main() 的范围内未声明。我总是可以将它们声明为全局变量,但可能有更好的方法来解决这个问题,我读到全局变量从长远来看并不是那么好。我不想使用类。那么大家有什么建议呢?
使用 C++/CLI 我想声明一个列表的列表。
在声明常规列表时,我写道:
List<String^>^ NameOfList = gcnew List<String^>(2);
因此,我试图声明一个列表的列表,如:
List<List<String^>^>^ AnotherName = gcnew List<List<String^>(2)>(2);
但是,Microsoft Visual Studio 抱怨右手边的论点说它无效。
注意,我可以创建一个空列表
List<List<String^>^>^ AnotherName;
这工作正常。有人知道这里有什么问题吗?
我寻找一种方法来初始化一个结构并在一行中返回这个结构指针。(便携式方式是最好的,但至少对于 gcc)
例如,我得到了一个函数:
void myfunc(const mystruct_t *ptr);
Run Code Online (Sandbox Code Playgroud)
一个结构定义:
typedef struct mystruct_t{
int field1;
const char *field2;
}mystruct_t;
Run Code Online (Sandbox Code Playgroud)
我可以 :
const mystruct_t mystruct={.field1=23, .field2="myname" };
myfunc(&mystruct);
Run Code Online (Sandbox Code Playgroud)
我有办法在一行中做到这一点吗?
就像是 :
myfunc(&{.field1=23, .field2="myname" });
Run Code Online (Sandbox Code Playgroud) 我想在if语句中声明一个变量并在同一语句中使用它.这根本不可能吗?
问题的例子(虽然不是实际用例......):
if ($test = array('test'=>5) && $test['test'] === 5) {
echo 'test';
} else {
echo 'nope';
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
注意未定义的变量:在第6行进行测试
这个问题可能很容易回答,但我在 numpy 文档中找不到明显的答案。
初始化二进制 numpy 数组时要选择的 dtype 是什么,如下所示:
array = np.zeros(8, dtype=np.int)
Run Code Online (Sandbox Code Playgroud) 晚上好,
我的宏有问题。我应该想出一个宏ENTRY,它将一个值放入一个数组中(scanf("%d",&ENTRY(x,i))已给出)。
我试过:#define ENTRY (a,b) (a[b-1]),但这没有用。
它创建了一个编译器错误,指出 a 和 b 未声明。
但我认为我不必声明宏中使用的变量,尤其是因为,例如:#define min (a,b) ((a)<(b)?(a):(b))在另一个程序中工作。
那么我在这里做错了什么?
#include <stdio.h>
#define N 3
#define ENTRY (a,b) (a[b-1])
int main(void)
{
int x[N],i;
float y[N];
for(i=1;i<=N;i++){ printf("x_%d = ",i);scanf("%d",&ENTRY(x,i));}
for(i=1;i<=N;i++){ printf("y_%d = ",i);scanf("%lf",&ENTRY(y,i));}
return 0
}
Run Code Online (Sandbox Code Playgroud) 在赋值之前,我应该在 C++ 程序的顶部声明变量吗:
int i;
std::string x;
std::string retval;
x = "foo";
i = 5;
retval = somefunction();
Run Code Online (Sandbox Code Playgroud)
或者,通过以下方式为变量赋值是否正确/可接受:
int i = 5;
std::string x = "foo";
std::string retval = somefunction();
Run Code Online (Sandbox Code Playgroud)
我是 C++ 的新手,我想知道 C++ 社区接受哪种方式。
我试图理解为什么当我使用 using 命名空间而不是显式声明命名空间外壳时,我的函数中存在歧义。
Book.h 头文件:
#ifndef MYBOOK_BOOK_H
#define MYBOOK_BOOK_H
namespace mybook
{
void showTitle();
void showTableOfContents();
}
#endif
Run Code Online (Sandbox Code Playgroud)
我的实现文件导致歧义错误:Book.cpp
#include "Book.h"
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
using namespace mybook;
void showTitle() {
cout << "The Happy Penguin" << endl;
cout << "By John Smith" << endl;
}
void showTableOfContents() {
cout << "Chapter 1" << endl;
cout << "Chapter 2" << endl;
}
Run Code Online (Sandbox Code Playgroud)
我的实现文件没有歧义错误:Book.cpp
#include "Book.h"
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
namespace …Run Code Online (Sandbox Code Playgroud) 为什么这段代码返回 500 和一些垃圾值?为什么除法函数不起作用?
#include <stdio.h>
int divmul(int v1, int v2)
{
int div,mul;
div =v1/v2;
mul=v1*v2;
return div,mul;
}
main()
{
int val1=50, val2=10;
printf("%d %d\n", divmul(val1,val2));
}
Run Code Online (Sandbox Code Playgroud) declaration ×10
c ×4
c++ ×3
definition ×3
variables ×3
struct ×2
c++-cli ×1
function ×1
if-statement ×1
list ×1
macros ×1
managed ×1
name-lookup ×1
namespaces ×1
numpy ×1
php ×1
php-7 ×1
pointers ×1
python ×1
return-value ×1