如果我想将已知或未知大小的数组作为函数参数传递,我对使用哪种语法感到困惑.
假设我有这些变体用于此目的:
void func1(char* str) {
//print str
}
void func2(char str[]) {
//print str
}
void func3(char str[10]) {
//print str
}
Run Code Online (Sandbox Code Playgroud)
使用这些中的每一个有什么利弊?
我正试图std::vector在我的项目中寻找替代品,我发现这std::queue就是我正在寻找的东西.
我有很多range-based loop用于迭代的函数,我试图尽可能地维护它.
我尝试编译一个range-based loopin std::queue但我得到的只是编译错误
错误:没有匹配函数来调用'begin(std :: queue&)'
不std::queue支持范围基本循环?
我确实尝试过,Google search但没有找到任何有关此问题的话题.
更新:
我的编译器是 GCC v4.7.1
-std=c++11 已启用
这是错误的测试代码:
std::queue<int> Q;
for (int i = 0;i < 10; ++i)
Q.push(i);
std::cout << "\nqueue contains: ";
for (auto i : Q)
std::cout << i << ", ";
Run Code Online (Sandbox Code Playgroud) 我是 SQL 新手,所以请考虑这个菜鸟问题。另外,我很尴尬地承认我无法在谷歌中搜索到正确的关键字,而且我已经没有时间了,所以我决定在这里提问。
代码:
select
*,
price * quantity as [Total price],
case
when [Total price]>100 and [Total price]<= 200 then '2%'
when [Total price]>200 and [Total price]<= 300 then '3%'
when [Total price]>300 and [Total price]<= 400 then '4%'
else '0%'
end as tax
from
grocery
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我想做的是尝试在执行 SQL 语句时创建一个临时变量,但是,这给了我错误
Error 1: could not prepare statement [1 no such column: Total price]
我怎么能这样做呢?
好的,我正在拔掉我所有的头发,但是,作为菜鸟,我确定存在几个问题。我想要一个矩阵,并通过执行基本的行操作,将其减少为行减少的梯形形式。我们假设(1)它是可解的,(2)一个唯一的解。没有检查零或任何东西;它只执行行操作。这是代码:
#include <iostream>
#include <cstdlib>
using namespace std;
void printmatrix(float A[][4]);
void RowReduce (float A[][4]);
int main() {
// answer should be { 2, 4, -3 }
float A[3][4] = {
{ 5, -6, -7, 7 },
{ 3, -2, 5, -17 },
{ 2, 4, -3, 29 }
};
printmatrix(A);
RowReduce(A);
}
// Outputs the matrix
void printmatrix(float A[][4]) {
int p = 3;
int q = 4;
for (int i = 0; i < p; i++) {
for …Run Code Online (Sandbox Code Playgroud) 我在谷歌上找不到任何简单的方法来做到这一点。我试试这个,但make说“没有找到目标,也没有找到生成文件。停止”。首先,我进入了lpng162/scripts 可以找到 gcc 生成文件的目录,但我是这个makefiles 的新手,我没有 vstudio 来轻松构建它。请帮忙。我无法让它工作。我假设这个问题是为了解决我将它链接libpng16.lib到我的 .dll 文件时的其他问题。这是我能想到的唯一解决方案。
我不知道正确的术语来描述它,这就是为什么我在谷歌中找不到它。我想要的只是缩放对象,特别是 2D 矩形,但将其保持在 XY 坐标上。
我目前的解决方案是这样的:
glTranslatef(x, y, 0);
glScalef(scaleX, scaleY, 0);
glTranslatef(-x, -y, 0);
drawRect(x, y, width, height, texture);
Run Code Online (Sandbox Code Playgroud)
我只是假装 glScale 作为 glRotate 在其中心旋转,它有点工作,但我可以看到缩放到其他值时其坐标的微小调整。
另外,最好使用 glTranslate 或仅在顶点上应用 x,y 等变量。从这两个切换时我看不出有什么区别,但我担心添加“推入和弹出矩阵”会影响场景中其他对象的行为和程序的性能,因为我将使用大量此类矩形。
更新:
所以你可以想象我想要实现的目标是多么简单

我想使用此代码定义一些通用指针(?但不是void指针):
class A
{
template<typename T>
using ptr = T*;
using ubyte = uint8_t;
public:
const ptr<ubyte>
getColor1() const {
return &colors[0];
}
const ubyte*
getColor2() const {
return &colors[0];
}
private:
ubyte colors[4];
};
Run Code Online (Sandbox Code Playgroud)
但是,getColor1()不会编译.这两个功能有什么区别?
gcc说:
error: invalid conversion from 'const ubyte* {aka const unsigned char*}' to 'A::ptr<unsigned char> {aka unsigned char*}' [-fpermissive]|
Run Code Online (Sandbox Code Playgroud)
更新:
删除的答案说我可以这样做:
//option 1
template<typename T>
using const_ptr = const T*;
Run Code Online (Sandbox Code Playgroud)
要么
//option 2
const ptr<ubyte>
getColor() //remove cv-qualifier
{
return &colors[0];
}
Run Code Online (Sandbox Code Playgroud)
来自option1, …
鉴于下图:

求content step与thumb height和成正比的值thumb step。
content step 是通过垂直位置的像素滚动量。
这里的公式是什么?
Object* pObject;
Object object;
pObject = &object;
Run Code Online (Sandbox Code Playgroud)
是否object与pObject被分配的内存一样多吗?
Object* pObject1, pObject2, pObject3;
Object object;
pObject1 = &object;
pObject2 = &object;
pObject3 = &object;
Run Code Online (Sandbox Code Playgroud)
和
pObject1 = &object;
pObject2 = pObject1;
pObject3 = pObject1;
Run Code Online (Sandbox Code Playgroud)
两者有什么区别?它是安全的取消引用pObject2,并pObject3在第二个例子?
我怎样才能做到这一点:
class Base
{
public:
int a, b, c;
void function();
...
};
class Derived1 :
private Base
{
public:
int d, e, f;
...
};
class Derived2 :
private Base
{
public:
void addObject(const Base* obj);
...
};
Run Code Online (Sandbox Code Playgroud)
请注意,我将其继承为私有
然后我想做这样的事情:
Base* d1 = new Derived1();
Base* d2 = new Derived2();
d2->addObject(d1);
Run Code Online (Sandbox Code Playgroud)
这是我从编译器那里得到的相关问题:
C:\test\test.cpp||In function 'void init()':|
C:\test\test.cpp|423|error: no matching function for call to 'Derived2::addObject(const Base*)'|
C:\test\test.cpp|423|note: candidate is:|
C:\test\test.h|29|note: void Derived2::addObject(const Base*)|
C:\test\test.h|29|note: no known conversion for argument …Run Code Online (Sandbox Code Playgroud) 在一些std库template的参数中,需要定义他/她自己的函数比较器less(a, b),more(a, b)那么std::some_template<T, *, myComparator()>,为什么呢?
c++ ×9
pointers ×3
arrays ×2
c++11 ×1
formula ×1
inheritance ×1
libpng ×1
makefile ×1
matrix ×1
mingw ×1
opengl ×1
polymorphism ×1
queue ×1
rectangles ×1
scrollbar ×1
sql ×1
sql-server ×1
stl ×1