我的课程有效,但我的教授说我的代码不正确,但表示他会在秋季学期找到原因......他在说什么?也许是不合适的东西?即使你是不正确的我会很感激你的大脑:)
void CResizableArray::SetSize( int intNewSize )
{
int intIndex = 0;
if( intNewSize < 0 ) intNewSize = 0;
if( intNewSize > intMAXIMUM_ARRAY_SIZE )
{
intNewSize = intMAXIMUM_ARRAY_SIZE;
}
//////////////////////////////////////
// ---> HUGE BUG HERE <--- //
// Code works but is WRONG //
// WHY IS THIS HELP ME FIND THE BUG //
//////////////////////////////////////
m_intArraySize = intNewSize;
m_paintValues = new int [m_intArraySize];
// Initialize to zero
for( intIndex = 0; intIndex < m_intArraySize; intIndex++ )
{
*( m_paintValues + …
Run Code Online (Sandbox Code Playgroud) 我已经提出了一些代码而且它没有编译.
错误1错误C2296:'^':非法,左操作数类型为'double'2
智能感知:表达式必须具有整数或枚举类型
我能想出的代码如下:
#include<iostream>
#include<cmath>
using namespace std;
void getnumbers();
void showmean();
void showdev();
double x1, x2, x3, mean, dev;
void getnumbers()
{
cout<<"Please enter three numbers\n";
cin>>x1,x2,x3;
}
void showmean()
{
mean=(x1+x2+x3)/3;
cout<<"The mean of"<<x1<<", "<<x2<<", "<<x3<<" is "<<mean<<endl;
}
void showdev()
{
dev=sqrt((x1 - mean)^2) + (x2 - mean)^2 + (x3 - mean)^2/3;
cout<<"The standard deviation of"<<x1<<", "<<x2<<", "<<x3<<" is "<<dev<<endl;
}
int main()
{
getnumbers();
showmean();
showdev();
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试理解以下代码:
lock_server::lock_server(class rsm *_rsm)
{
//code
}
Run Code Online (Sandbox Code Playgroud)
我知道这是该类的构造函数,但我不理解它的论点.我猜这是一个指向一个类的指针(名称为_rsm)?那有意义吗?我在哪里可以找到关于此的文档?
我打印出std :: string的字节表示时遇到奇怪的错误,而std :: wstring工作正常.
std::string str = "mystring";
unsigned short* vtemp = (unsigned short*)str.c_str();
for(int i=0; i<str.length(); ++i)
{
cout << (unsigned short)((unsigned char)vtemp[i]) << " ";
}
cout << endl;
Incorrect Output: 109 115 114 110 0 204 204 204
wstring wstr(str.length(), L' ');
std::copy(str.begin(), str.end(), wstr.begin());
vtemp = (unsigned short*)wstr.c_str();
for(int i=0; i<wstr.length(); ++i)
{
cout << (unsigned short)((unsigned char)vtemp[i]) << " ";
}
cout << endl;
Correct Output: 109 121 115 116 114 105 110 103 …
Run Code Online (Sandbox Code Playgroud) 我有这样的事情:
class A {
void add (A* a) {
//add a to a vector<A*>
}
virtual void draw() = 0;
}
class B : public A {
void tick() {}
void draw() {}
}
class C : public A {
void draw() {}
}
Run Code Online (Sandbox Code Playgroud)
现在我想要做的是这样的循环:
for(int i=0; i<vector.size(); i++) {
vector[i]->tick();
}
Run Code Online (Sandbox Code Playgroud)
问题是,并非此向量中的所有元素都具有该tick()
方法,但我仍然希望将它们放在同一个向量中,因为我还希望能够遍历向量并调用draw()
所有元素.有什么方法可以解决这个问题吗?我正在考虑使用另一种载体,但我宁愿不这样做.
我正在尝试编写一个Bash别名(或脚本),其中列出了当前正在运行的某些特定程序.
在我的.bashrc
我已$MY_BIN_PATH
包含所有感兴趣的节目给我的目录:
export MY_BIN_PATH=/root/repo/bin
$ > echo $MY_BIN_PATH
Run Code Online (Sandbox Code Playgroud)
/根/回购/ bin中
列出该目录显示了许多文件.我希望每个grep
输出ps -ef
:
for i in `ls $MY_BIN_PATH`;
do
ps -ef | grep $i | egrep -v "grep|md_m|avahi";
done
Run Code Online (Sandbox Code Playgroud)
无论哪个(如果有)文件$MY_BIN_PATH
正在运行,输出始终是:
/bin/grep: Unmatched [ or [^
/bin/grep: Unmatched [ or [^
/bin/grep: Unmatched [ or [^
/bin/grep: Unmatched [ or [^
/bin/grep: Unmatched [ or [^
Run Code Online (Sandbox Code Playgroud)
它看起来像不知何故值$i
之间的管道被"遗忘" ps -ef
和grep $i
.
我在这里做错了什么,我怎样才能让它发挥作用?
考虑以下代码:
#include "stdio.h"
typedef struct CustomStruct
{
short Element1[10];
}CustomStruct;
void F2(char* Y)
{
*Y=0x00;
Y++;
*Y=0x1F;
}
void F1(CustomStruct* X)
{
F2((char *)X);
printf("s = %x\n", (*X).Element1[0]);
}
int main(void)
{
CustomStruct s;
F1(&s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在运行时,在调用函数结束时,F1
通过使用不同的编译器得到不同的结果.
(*X).Element1[0] = 0x1f00
在一些编译器和(*X).Element1[0] = 0x001f
另一个编译器.
我很清楚它是一个字节序问题.
是否有任何编译器选项或解决方法使用,以便我得到(*X).Element1[0] = 0x001f
所用的编译器?
我有一个向量,其中包含一个类对象.我是这样宣布的
vector<Aircraft> queue;
Run Code Online (Sandbox Code Playgroud)
我擦除了向量中的第一个元素.但它似乎仍然是向量中的第一个元素.我这样做了
queue.erase(queue->begin());
cout<<queue.size(); //printed 0
Aircraft temp = queue.front();
cout<<temp.id; //expected a segfault error
Run Code Online (Sandbox Code Playgroud)
擦除第一个元素后,向量的大小为0(只有一个元素).当我试图查看temp(飞机)的id时,我期待一个seg错误错误,因为我从矢量中删除了第一个元素,之后大小为零.我甚至清除了向量,但'queue.front '仍然返回对象,而cout语句仍然打印对象的id.
该代码应该计算输入文本文件中的a,b,c,d,e和f字符的数量,并将输出打印到第二个文本文件中.当我运行代码时,它会创建输出文件,但不会在其中写入任何内容.
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
int main(){
// establish counters for the number of each character
char x;
int acount=0;
int bcount=0;
int ccount=0;
int dcount=0;
int ecount=0;
int fcount=0;
ifstream iFile("plato.txt"); //define & open files
ofstream oFile("statistics.txt");
if(!iFile){
cout<<"The file could not be opened.";
exit(1);
}
if(!oFile){
cout<<"The file could not be opened.";
exit(1);
}
iFile>>x;
while(!iFile.eof()){
if(x=='a'||x=='A'){
acount++;
}
else if(x=='b'||x=='B'){
bcount++;
}
else if(x=='c'||x=='C'){
ccount++;
}
else if(x=='d'||x=='D'){
dcount++;
}
else if(x=='d'||x=='D'){
dcount++;
}
else …
Run Code Online (Sandbox Code Playgroud) 我有两个类,如下所示(我试图尽可能抽象示例):
#include <iostream>
using namespace std;
class foo1
{
public:
foo1() {};
virtual ~foo1() {};
void Method1() { Method2(); }
virtual void Method2() { cout<<"parent";}
};
class foo2 : public foo1
{
public:
virtual void Method2() { cout<<"child";}
};
int main()
{
foo2 a = foo2();
a.Method1();
}
Run Code Online (Sandbox Code Playgroud)
我收到了"父母"的消息.所以Method1()
的foo2
执行foo1::Method2()
.
我需要使用什么来foo2::Method1
调用它们foo2::Method2
?
如何将int的值复制到新的const int?
由此:
int start= 343;
Run Code Online (Sandbox Code Playgroud)
进入这个:
const int end = start;
Run Code Online (Sandbox Code Playgroud)
如何做到这一点,任何一个例子?我真的不知道如何,请帮助我 -
for循环:
//for testing, this is usually a value of about 27
int test = level.PathLookupVectors()[globalNodePositionIndex][globalNodeChoice].size();
for (int i = 0; i < level.PathLookupVectors()[globalNodePositionIndex][globalNodeChoice].size(); i++)
{
//adds the correct nodes to the search
search.push_back(level.Nodes()[level.PathLookupVectors()[globalNodePositionIndex][globalNodeChoice][i]].Index());
}
Run Code Online (Sandbox Code Playgroud)
它是一个64位系统.
我在调试时得到整数'i'的非常奇怪的结果.它应该初始化为0,但由于某种原因,它是一个非常高的数字,这反过来意味着for循环没有执行.