我正在通过GDB对用C++编写的项目进行调试,并发现在GNU C++编译器中没有警告或错误地修改了const.
这不是我正在调试的程序,但这是我目睹的行为的一个例子:
#include <iostream>
int main(int argc, char *argv[]) {
const int x = 10;
int *px = (int *)&x;
++*px;
std::cout << "*px: " << *px << "\n";
std::cout << "x: " << x << "\n";
for (int i = 0; i < x; ++i)
std::cout << i+1 << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不能代表其他编译器,因为我只使用GNU C++编译器4.9.2版进行了测试.为什么允许这样的事情?这打破了const对象的整个点.
我用g ++ main.c -Wall -Werror编译了上面的代码
输出:
*px: 11
x: 10
1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud) 我试图创建通用的流类持有者,但似乎我无法传递std::cout给它,代码:
#include <iostream>
struct x
{
std::ostream &o;
x(std::ostream &o):o(o){}
};
int main()
{
x(std::cout);
x.o<<"Hi\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时失败:
c++ str.cc -o str -std=c++11
str.cc: In function ‘int main()’:
str.cc:11:14: error: invalid use of qualified-name ‘std::cout’
str.cc:12:4: error: expected unqualified-id before ‘.’ token
Run Code Online (Sandbox Code Playgroud)
为什么?
我想提供一些对象的一次性配置.
这段代码:
class Foo
{
public:
static struct Bar {
bool a = true;
int b = 69;
} bar;
};
int main(int argc, char **argv)
{
Foo::bar.a = false;
}
Run Code Online (Sandbox Code Playgroud)
编译得很好:
$ g++ -c -std=gnu++11 main.cpp
Run Code Online (Sandbox Code Playgroud)
但链接器抱怨缺少符号:
$ g++ main.o
Undefined symbols for architecture x86_64:
"Foo::bar", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?
和
实现同样目标的更好方法是什么?
我已经完成了C++程序,其中我的基类即Shape完成获取数据的工作,以及其他两个派生类Triangle并Rectangle 计算区域.问题是我将该区域视为一些垃圾值.我已经完成了代码,请看一下并指导我.谢谢
#include<iostream>
using namespace std;
class Shape{
protected: double b,h;
public:void get_data()
{
cout<<"Enter the height\n";
cin>>h;
cout<<"Enter the breadth\n";
cin>>b;
}
virtual void display_area(){}
};
class Rectangle:public Shape
{
public:void display_area(){
cout<<"\n\nArea of Rectangle:" << b*h;
}
};
class Triangle:public Shape
{
public:void display_area(){
cout<<"\n\nArea of Triangle:"<<0.5*b*h;
}
};
int main()
{
Shape s;
Triangle t;
Rectangle r;
Shape *ptr;
ptr=&s;
ptr->get_data();
ptr=&t;
ptr->display_area();
ptr=&r;
ptr->display_area();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我做了一个简单的程序来计算矩阵,这里是代码:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int result[3] = {0,0,0};
int matrixc[3][6] = {
{0,0,0,0,0,1},
{0,0,0,1,1,1},
{1,0,1,0,0,1}
};
for(int x=0;x <3;x++)
{
for(int y=0;y < 6;y++)
{
result[x] += (matrixc[x][y] * pow(2,6-y));
}
cout << result[x] << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
输出是我想要的,它是:2,14,and 82.但是,当我删除整数数组中的初始化时result:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int result[3]; //deleted initialization
int matrixc[3][6] = {
{0,0,0,0,0,1},
{0,0,0,1,1,1},
{1,0,1,0,0,1}
};
for(int x=0;x <3;x++)
{
for(int y=0;y < …Run Code Online (Sandbox Code Playgroud) 我不明白为什么unsigned int的输出对于以下代码是负的.就像一个签名的int.
uint32_t yyy=1<<31;
printf("%d\n",yyy);
Run Code Online (Sandbox Code Playgroud)
输出是:
-2147483648
是的-2^31.
在C++中,
i = ++++j;
Run Code Online (Sandbox Code Playgroud)
在代码中工作正常,但是当我使用时,
i = j++++;
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Operand for operator "++" must be an lvalue.
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误?
所以我正在制作一个游戏,你是老板和招聘员工,我以前用过rand,但由于某种原因它现在不能正常工作:
生成随机数的代码:
class employee
{
public:
int getSkills(int askingPrice)
{
srand((unsigned)time(0));
switch(askingPrice)
{
case 1: //point of this is so if askingPrice is from 1 to 5 it will execute the same code
case 2:
case 3:
case 4:
case 5:
skills = rand() % 5 + 1;
}
return skills;
}
int returnSkills()
{
return skills;
}
int getPaid(int askingPrice)
{
srand((unsigned)time(0));
switch(askingPrice)
{
case 1:
case 2:
case 3:
case 4:
case 5:
paid = rand() …Run Code Online (Sandbox Code Playgroud) main()
{
char *c="abhishek";
int i;
c[2]=90;
for(i=0;i<12;i++)
{
printf("%c",c[0])
}
}
Run Code Online (Sandbox Code Playgroud)
这里的输出是abZishek.但这会导致总线错误,因为这是一个字符串文字,我们无法更改其值.为什么c变化的价值?
我对C中的postfix和前缀运算符优先级感到困惑,任何帮助和提示都会有所帮助.
我会在这里粘贴我的测试代码:
#include <stdio.h>
int a = 0;
int main(int argc, char *argv[])
{
if (++a & 0x01) // prefix version
// if (a++ & 0x01) // postfix version
{
printf("++ first\n");
}
else
{
printf("& first\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我现在可以理解,在postfix版本中,虽然postfix ++有一个更大的优先级,但是a++会返回0到这里&,0x01并且会a在此表达式后增加值.
但我无法理解的是为什么在前缀版本中,为什么++a首先进行评估?运算符优先级表指示prefix ++并&具有相同的优先级,此外,它们的关联性为right-to-left.这种方法&不应该先评估吗?
编辑: 我正在使用的图表:C运算符优先表