大家好我是非常新的c ++,可能在我试图解决这个问题上.我所要求的是一个很好的视觉解释和解决我的错误甚至更好的修改源代码.感谢投资的每个人对我的问题感兴趣.
下面是问题:设计一个名为rectangle的类来表示一个矩形.该课程必须包含:
绘制类的UML图.实现类.编写一个测试程序,创建两个矩形对象.将宽度4和高度40指定给第一个对象,将宽度指定为3.5,将高度35.9指定给第二个对象.显示两个对象的属性并查找其区域和周长.
以下是我到目前为止的情况:
#include <iostream>
using namespace std;
class Rectangle
{
public:
double height;
public:
double width;
Rectangle()
{
width = 4;
}
rectangle(double newArea)
double height;
height()
(
height = 40
{
{
area = height* width;
}
double getArea()
{
return Area;
}
bool isOn()
{
return on;
}
double getPerimeter()
{
return Perimeter;
}
void setPerimeter(double radius)
cout << "The area of the Rectangle"
<< rectangle1.area<<"is"<<rectangle1.getArea()<< endl;
cout<<"The …Run Code Online (Sandbox Code Playgroud) 仍然与C语句中的声明和定义混淆:如果头文件如下:
#ifndef _BASIC_H_
#define _BASIC_H_
void test();
extern int i; //define or declare
#endif
Run Code Online (Sandbox Code Playgroud)
两个源文件f1.c和f2.c包含这个头,然后一个源文件需要定义变量"i".
但如果头文件是这样的:
#ifndef _BASIC_H_
#define _BASIC_H_
void test();
int i; //define or declare
#endif
Run Code Online (Sandbox Code Playgroud)
和两个源文件f1.c和f2.c,包含这个头,没有在任何文件中定义变量"i",当我使用变量时它仍然会通过.
我的问题是定义变量的时候.
谢谢
我有一个简单的代码如下:
class B;
class A{
B b;
};
class B{
public:
B(){
}
};
Run Code Online (Sandbox Code Playgroud)
在A类的定义中,我有一个B型属性.使用MS Visual Studio进行编译,我遇到以下错误:
error C2079: 'A::b' uses undefined class 'B'
Run Code Online (Sandbox Code Playgroud)
由于某些原因,我不能把B类的定义放在A类之前.任何的想法?
我有两个需要指向彼此的对象......唯一的问题是,由于它们是按特定顺序声明的,因此一个或另一个不知道存在的另一个对象.例如:
...
#define foobar_h
class Foo {
Bar* b;
};
class Bar {
Foo* f;
};
...
Run Code Online (Sandbox Code Playgroud)
我如何声明这些类,以便他们能够快乐地引用彼此?
我有4个文件:
shared.h:
#ifndef SHARED_H
#define SHARED_H
int* sth;
#endif
Run Code Online (Sandbox Code Playgroud)
something.h:
#ifndef SOMETHING_H
#define SOMETHING_H
class foo
{
public:
void printVar();
};
#endif
Run Code Online (Sandbox Code Playgroud)
something.cpp:
#include <iostream>
#include "something.h"
#include "shared.h"
using namespace std;
void foo::printVar()
{
cout<<"Foo: "<<*sth<<endl;
};
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <cstdlib>
#include <iostream>
#include "shared.h"
#include "something.h"
using namespace std;
int main(int argc, char *argv[])
{
sth=new int(32);
foo x;
cout<<"Main: "<<*sth<<endl;
x.printVar();
system("PAUSE");
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
编译器返回*sth的multipe定义;
我在*sth中添加了静态修饰符并编译,但崩溃了.我更改了打印以打印指针的地址,我返回了程序:
Main: 0x3e0f20
Foo: 0
Run Code Online (Sandbox Code Playgroud)
为什么没有分配foo的指针?我想在main中只分配一次,然后在其他文件中共享...我该怎么做?它是extern修饰符的东西吗? …
我正在研究一个简单的自上而下的射击游戏,并希望将我的船只移到一个单独的ShipManager类,在那里我可以从一个位置管理所有这些.但是,在启动时,我的playerShip上出现了一个链接器错误:
错误LNK2001:未解析的外部符号"public:static class Ship*ShipManager :: playerShip"
ShipManager.h如下所示:
class Ship;
class ShipManager
{
public:
static Ship* playerShip;
};
Run Code Online (Sandbox Code Playgroud)
我在ShipManager .cpp中什么都没有.我错过了什么?我使用此代码的唯一其他地方是在我的游戏类中,我实际上正在调用ShipManager :: playerShip,我没有在那里得到任何错误.
我在我的game.cpp中包含"ShipManager.h",所以应该找到它吗?我有一种感觉,我在这堂课中忘记了一些简单的事情.
我知道它总是首先执行main()函数,然后函数调用将程序指向其他函数.如果有什么功能被称为*之前*的主要()函数?什么时候会被执行?
我有一个程序(我从互联网上下载),并在main()之前有函数调用.
现在我不知道它们是什么,如果只在main()中执行(以及在main中调用的函数).
这是该计划的片段:
static void set_level_indices (VideoParameters *p_Vid);
static void chroma_mc_setup (VideoParameters *p_Vid);
static void init_img (VideoParameters *p_Vid);
static void init_encoder (VideoParameters *p_Vid, InputParameters *p_Inp);
static int init_global_buffers (VideoParameters *p_Vid, InputParameters *p_Inp);
static void free_global_buffers (VideoParameters *p_Vid, InputParameters *p_Inp);
static void free_img (VideoParameters *p_Vid, InputParameters *p_Inp);
static void free_params (InputParameters *p_Inp);
static void encode_sequence (VideoParameters *p_Vid, InputParameters *p_Inp);
*(SOME FUNCTION DECLARATIONS OMITTED)*
int main(int argc, char **argv)
{
init_time();
#if MEMORY_DEBUG …Run Code Online (Sandbox Code Playgroud) 这可能看起来很简单,但这个问题在很多方面让我感到痒痒.
我的问题是关于c中变量的声明和辩护.
互联网上实际上有很多关于这个的解释,并且这个问题不仅有一个解决方案,因为在这个问题上有许多观点.我想知道这个问题的明确存在.
int a;
Run Code Online (Sandbox Code Playgroud)
只是拿这个是一个声明或定义?,这个当我使用时printf,它具有0作为值和地址2335860.但是如果这个声明那么为此分配内存的方式.
int a;
int a;
Run Code Online (Sandbox Code Playgroud)
当我这样做时,它说先前的'a'声明在这里并重新声明'a'没有联系.
一些消息来源说c中允许重新声明,有些人说不是真的是什么?
我是C的新手,我正在尝试使用多维数组.我有以下示例,我正在尝试初始化多维数组:
char matrix[5][10];
matrix[0] = {'0','1','2','3','4','5','6','7','8','9'};
matrix[1] = {'a','b','c','d','e','f','g','h','i','j'};
matrix[2] = {'A','B','C','D','E','F','G','H','I','J'};
matrix[3] = {'9','8','7','6','5','4','3','2','1','0'};
matrix[4] = {'J','I','H','G','F','E','D','C','B','A'};
Run Code Online (Sandbox Code Playgroud)
乍一看似乎这种类型的声明是有效的,因为多维数组是一个数组数组; 但是,这个例子无法正确编译,我不完全确定为什么.
我知道我能够使用以下表示法初始化多维数组:
char matrix2[5][10] =
{
{'0','1','2','3','4','5','6','7','8','9'},
{'a','b','c','d','e','f','g','h','i','j'},
{'A','B','C','D','E','F','G','H','I','J'},
{'9','8','7','6','5','4','3','2','1','0'},
{'J','I','H','G','F','E','D','C','B','A'},
};
Run Code Online (Sandbox Code Playgroud)
但是,如果我在声明时不知道数组的内容并希望稍后用数据填充此数组,该怎么办?我可以按如下方式初始化每个元素:
matrix[0][0] = '0';
matrix[0][1] = '1';
matrix[0][2] = '2';
etc....
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能使用我原来的方法声明每个数组:
matrix[0] = {'0','1','2','3','4','5','6','7','8','9'};
matrix[1] = {'a','b','c','d','e','f','g','h','i','j'};
etc...
Run Code Online (Sandbox Code Playgroud)
我尝试使用strcpy如下:
strcpy(matrix[0], "012345678");
strcpy(matrix[1], "abcdefghi");
Run Code Online (Sandbox Code Playgroud)
看起来如果多维数组是一个以空字符结尾的字符串数组,这可能会起作用,但是它会等同于整数或其他数据结构的多维数组.任何帮助表示赞赏.谢谢.
考虑一下代码:
int main(void)
{
int a;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这int a;是一个定义,因为它会导致存储被保留.引用C标准(N1570委员会草案 - 2011年4月12日):
6.7/5语义 声明指定了一组标识符的解释和属性.标识符的定义是该标识符的声明:
- 对于一个对象,导致为该对象保留存储;
...
问题出在这里:编译器可能会优化存储,因为我们没有使用变量.那么int a;是宣言吗?而如果我们做printf("%p", &a)的main(void)-当然现在编译器必须分配存储空间,所以是声明/定义的概念依赖于你是否以后使用标识符或没有?