C++问题 error C2065: 'flag' : undeclared variableidentifier请教一下错在哪里

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.
This is a very basic problem, I need to find the area and volume of a sphere but I am getting this error:
error C2065: 'v' : undeclared identifier
error C2065: 'a' : undeclared identifier
Here is my program:
#include &iostream&
#include &cmath&
#include &iomanip&
int computeSphere(int r) {
double a,v;
a = 4*3.14* pow(r,2);
v = (4/3) * 3.14 * pow(r,3);
return a,v;
int main() {
cout && "Enter the radius: ";
cout && fixed && setprecision(2);
computeSphere(r);
cout && "The area of a sphere of radius " && r && " is " && a && " and its ";
cout && "volume is ";
the question says that The function should not perform any I/O operations.
so how can I show the results ??
3,93872248
Two problems:
You can't return multiple values from a function.
You aren't doing anything with the return value where you call the function.
One way to solve the first problem is to define a struct:
struct SphereStuff
SphereStuff computeSphere(double r)
stuff.a = ...;
stuff.v = ...;
int main()
SphereStuff s = computeSphere(42);
std::cout && s.a && ", " && s.v && "\n";
Note also how I'm "collecting" the return value of the function (on the line marked with "***").
166k16308470
Divide it into two functions
double Volume (int r) { return (4/3) * 3.14 * pow(r,3); }
double Area (int r) { return 4*3.14* pow(r,2); }
And then use it like that
double v = Volume(r);
double a = Area(r);
#include &iostream&
#include &cmath&
#include &iomanip&
double calc_a(int r){
return 4*3.14* pow(r,2);
double calc_v(int r){
return (4/3) * 3.14 * pow(r,3);
int main()
double a,v;
cout && "Enter the radius: ";
a = calc_a(r);
v = calc_v(r);
cout && fixed && setprecision(2);
cout && "The area of a sphere of radius " && r && " is " && a && " and its ";
cout && "volume is ";
15.1k43253
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Top questions and answers
Important announcements
Unanswered questions
By subscribing, you agree to the
Stack Overflow works best with JavaScript enabledStack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.
I am implementing a header file "IVideoPlayer.h" and I have created an abstract class "IVideoPlayer".
class IVideoPlayer
// Initialization
virtual bool Load(const char* pFilePath, bool useSubtitles = false) = 0;
virtual bool Start() = 0;
virtual bool Stop() = 0;
And the functions of that are defined in file "VideoPlayer.cpp"
#include "stdafx.h"
#include "IVideoPlayer.h"
#include &dshow.h&
HRESULT hr = CoInitialize(NULL);
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent
*pEvent = NULL;
class VideoPlayer:public IVideoPlayer
bool Load(const char* pFilePath, bool useSubtitles = false)
EPlaybackStatus var1 = PBS_ERROR;
// Initialize the COM library.
if (FAILED(hr))
printf("ERROR - Could not initialize COM library");
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
printf("ERROR - Could not create the Filter Graph Manager.");
hr = pGraph-&QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph-&QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph-&RenderFile(L"G:\edit.wmv", NULL);
bool Start()
if (SUCCEEDED(hr))
// Run the graph.
hr = pControl-&Run();
if (SUCCEEDED(hr))
// Wait for completion.
pEvent-&WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
bool Stop()
pControl-&Release();
pEvent-&Release();
pGraph-&Release();
CoUninitialize();
And to check the header file I have created file sample.cpp
#include "stdafx.h"
#include "IVideoPlayer.h"
#include &stdio.h&
#include &conio.h&
int main(void)
h.Load("G:\hila.wmv");
The errors are:
1 error C2065: 'VideoPlayer' : undeclared identifier
2 error C2146: syntax error : missing ';' before identifier 'h'
3 error C2065: 'h' : undeclared identifier
4 error C2065: 'h' : undeclared identifier
5 error C2228: left of '.Load' must have class/struct/union
Why compiler is showing it as undeclared identifer?
Any help is accepted. Thanking you in advance
You never include any header files which defined the std namespace, so using that (undefined) namespace results in an error. You also don't include any header which defines the VideoPlayer class, mostly because you have decided to put the class definition in a source file instead of a header file.
The above accounts for the two first errors. The remaining errors are follow up errors because of the second error (VideoPlayer not defined).
You need to make a header file where you put the VideoPlayer class definition, much like the header file for the IVideoPlayer class. You put the implementation of the VideoPlayer member functions in the source file. Then include the header file in the source file where you need the VideoPlayer class.
158k12102180
There is no definition of name std in your program because you do not use any standard header that contains the definition of namepsace std.
At least change this directive
#include &stdio.h&
#include &cstdio&
Also you have to place the definition of class 'VideoPlayer in a header and include this header in module sample.cpp
You need to add #include
at the top.
'std' namespace is defined in iostream library.
Also you need to do a forward declaration of the "Video Player" class in your main cpp file .
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledStack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.
Possible Duplicate:
Im getting the error sprite.h(20): error C2065: 'Component' : undeclared identifier when I try to compile (I got a couple other files as well). Below is the sprite.h file. I cant for the life of me figure out what is causing this problem.
#ifndef SPRITE_H
#define SPRITE_H
#include "Image.h"
#include "Rectangle.h"
#include &string&
#include &SDL.h&
#include &vector&
#include "Component.h"
namespace GE2D {
class Sprite {
Sprite(Image *i);
Sprite(Image *i, int x, int y);
Sprite(char *file, bool transparentBg, int x, int y, int w, int h);
virtual ~Sprite();
virtual void tick(SDL_Surface *screen, std::vector&Sprite*&* sprites, std::vector&Component*&* components);
virtual void handleEvent(SDL_Event eve);
virtual void draw(SDL_Surface *screen);
void setPosition(int x, int y);
const Rectangle& getRect()
const Image& getImage()
const Sprite& operator=(const Sprite& other);
Sprite(const Sprite& other);
protected:
In the .cpp file tick() is defined like this:
void Sprite::tick(SDL_Surface *screen, std::vector&Sprite*&* sprites, std::vector&Component*&* components) {}
tick() is supposed to take two vectors like they do now, but maybe there's a better way to do that which might solve this problem?
As requested, here is Component.h as well:
#ifndef COMPONENT_H
#define COMPONENT_H
#include "Rectangle.h"
#include "Component.h"
#include "Sprite.h"
#include &vector&
#include &SDL.h&
namespace GE2D {
class Component {
Component();
virtual ~Component();
virtual void draw(SDL_Surface *screen) = 0;
virtual void tick(SDL_Surface *screen, std::vector&Sprite*&* sprites, std::vector&Component*&* components) = 0;
virtual void handleEvent(SDL_Event eve) = 0;
const Rectangle& getRect()
protected:
Component(int x, int y, int w, int h);
marked as duplicate by , , , ,
This question has been asked before and already has an answer. If those answers do not fully address your question, please .
Sprite.h includes Component.h which includes Sprite.h, giving a circular dependency which can't be resolved.
Luckily, you don't need to include the headers at all. Each class only refers to a pointer to the other class, and for that a simple declaration is enough:
176k10216399
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enablederror C2065: 'flag' : undeclared identifier 请教一下错在哪里,非常感谢!_百度知道
error C2065: 'flag' : undeclared identifier 请教一下错在哪里,非常感谢!
#include&stdio.h&#include&math.h&void main(){ double a,b,c,d,e;
printf(&请输入三角形三边长:&);
scanf(&%f%f%f&,&a,&b,&c);
(a+b&c&&a+c&b&&b+c&a)
{ d=(a+b+c)/2;
e=sqrt(d*(d-a)*(d-b)*(d-c));
if(a==b&&a==c)
printf(&等边三角形\n&);
if(a==b||a==c||b==c)
printf(&等腰三角形\n&);
if(a*a+b*b==c*c||a*a+c*c==b*b||c*c+b*b==a*a)
printf(&直角三角形\n&);
printf(&普通三角形\n&);
else flag=0 if (flag) {
printf(&三角形面积为:%.2f&,e); }
printf(&这三条边不能构成三角形\n&);}
提问者采纳
if (flag),你这个flag没有定义,也没有赋值
不是默认上方吗?怎么改呢?
你在程序一开始定义就可以了
搞定了,其实还有一个问题,就是double后面应该是%lf.谢啦~
提问者评价
其他类似问题
为您推荐:
c2065的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 cout undeclared 的文章

 

随机推荐