当我使用常量nVar而不是数字时,我得到以下错误.
constants.h:
extern const unsigned int nVar;
Run Code Online (Sandbox Code Playgroud)
constants.cpp:
#include "constants.h"
const unsigned int nVar = 5;
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#pragma once
#include "constants.h"
void foo(const double q[nVar])
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
编译:
数组常量不是']'标记之前的整数常量
预期')'在'之前','令牌
在'const'之前预期的nonqualified-id
下面有两个循环.第一个效果很好,而第二个是无限循环.为什么?
for (unsigned int i=0; i<3; ++i)
{
std::cout << "i= " << i << std::endl; // this gives proper result
}
for (unsigned int i=3; i>=0; --i)
{
std::cout << "i= " << i << std::endl; // infinite loop
}
Run Code Online (Sandbox Code Playgroud) 假设a Domain存储指针Shape.确切的形状(Triangle或Rectangle)在编译时是未知的,并且在读取输入后将清楚.在运行时,我可能需要访问派生结构的变量,但这是不可能的,因为指针指向基础结构.我发现了另一个解决方案是做"接通型",而是在回答指出,它不鼓励在这里.他也说过
当您使用多态时,您不需要关心基类引用/指针背后的内容.
那么在这种情况下我会关心,所以听起来我不应该使用多态.我想我在下面做的是一个糟糕的设计,但那么解决这个问题的好设计是什么?
struct Shape
{
int common_variable;
};
struct Triangle: Shape
{
int triangle_specific_variable;
};
struct Rectangle: Shape
{
int rectangle_specific_variable;
};
struct Domain
{
Shape* shape;
};
int main()
{
Domain domain;
//domain.shape = new Triangle(); // depends on input.
//domain.shape = new Rectangle(); // depends on input.
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图在C中创建一个程序,使用指针计算两个int数组的总和.这是我想要做的一个例子:
int a[] = {1,2,3,4}
int b[] = {1,2,3,4,5,6}
int c[] = sumArrays(a,b,4,6)
Output : c = {2,4,6,8,5,6}
Run Code Online (Sandbox Code Playgroud)
问题是我的输出不同,它显示:
Output : c = {2,4,6,8}
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么以及如何纠正它?这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int* sumArrays(int *arr1, int *arr2, int dim1,int dim2)
{
int *ret = NULL;
if(dim1<dim2) ret = (int*) malloc(dim2*sizeof(int));
else ret = (int*) malloc(dim1*sizeof(int));
if(ret)
{
if(dim1<dim2) {
int i = 0;
for (i = 0;i < dim1;i++) {
ret[i] = arr1[i] + arr2[i];
}
for (i = dim1; i < dim2;i++) …Run Code Online (Sandbox Code Playgroud) 我有一个结构:
struct myStruct {
// there are other members as well
float U, V, W;
}
std::vector <myStruct> myVec;
myVec.resize(num);
Run Code Online (Sandbox Code Playgroud)
我想只将myVec [].U传递给所有myVec函数.
以下哪项更快?
方法1:
int foo (int A, int B) {
// write equations
}
Run Code Online (Sandbox Code Playgroud)
方法2:
int operator| (int A, int B) {
// write equations
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试优化我的代码,以便在运行时尽可能快地运行.我通过切换几个优化选项来比较VS和Intel,但我没有注意到显着的差异.但是,由于我的处理器是英特尔,它应该更快.你建议哪些优化开关最大化速度?
在下面的代码,push_back()一std::ref成std::vector<reference_wrapper<Type>>行之有效然而,分配std::ref到reference_wrapper<Type>不起作用.为什么?
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
struct Type {};
int main()
{
Type t1;
vector<reference_wrapper<Type>> t2;
t2.push_back( ref(t1) ); // OK
//reference_wrapper<Type> t3; // error: no matching function for call to std::reference_wrapper<Type>::reference_wrapper()’
//t3 = ref(t1);
return 0;
}
Run Code Online (Sandbox Code Playgroud) reference_wrapper当我resize()在下面的向量时,指向何处?这是一个未定义的行为吗?我该怎么做才能安全?
std::vector < std::reference_wrapper <int> > vec;
vec.resize(10);
Run Code Online (Sandbox Code Playgroud)