Error C2065: 'Cout':Undeclared Identifier

C++ cout gives undeclared identifier

stdafx.h shall be the first include directive in your source file.

Switch files and convert the second include to <>, as other suggested.

#include "stdafx.h"
#include <iostream>

See this post for more information.

Error: 'cout' : undeclared identifier; though I've included iostream header file in program

The cout stream is defined in the std namespace. So to name it you write:

std::cout

If you want to shorten this to cout then you can write

using namespace std;

or

using std::cout;

before writing cout.

Any good documentation source will tell you which namespace contains an object. For instance: http://en.cppreference.com/w/cpp/io/cout

C++ How to declare out of constructor (Error C2065 undeclared identifier)

Here's my version of your code. Untested, should at least compile though.

template <class T, class P>
class map_swapper
{
public:
map_swapper(map <T, P>& Cmap) : copym(Cmap)
{
}

void swap(const T &t1, const T &t2)
{
P a = copym[t1];
copym[t1] = copym[t2];
copym[t2] = a;
}
private:
map<T, P> & copym;
};

Still think using a class to do this is a bit weird.

error C2065: 'lO' : undeclared identifier for declaring a vector value?

You're using the letters l (ell) and o (oh) instead of the numbers 1 and 0. You need to use numbers for immediate values.

Undeclared Identifier C2065 P

because you seperated with comman, so the compiler will consider the code as

unsigned char choice = p; 
unsigned char m;
unsigned char q;

So the p is undeclared

error C2065: undeclared identifier

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
{
double a;
double v;
};

SphereStuff computeSphere(double r)
{
SphereStuff stuff;
stuff.a = ...;
stuff.v = ...;
return stuff;
}

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 "***").

error C2065: 'firstName': undeclared identifier

StudentPrint is not a member function of Student and can thus not access those variables. Either make it a member function of Student, or better yet, use the 'get' functions you've defined (after you've actually implemented them).

void StudentPrint(const Student& student) {
cout << "student ID: " << student.GetStudentID() << endl;
cout << "age: " << student.GetAge() << endl;
cout << "First name: " << student.GetFirstName() << endl;
cout << "Last name: " << student.GetLastName() << endl;
cout << "Email: " << student.GetEmailAddress() << endl;
}

As a side note; StudentPrint cannot be marked as const as it's not a member function, but a free function. Only member functions can be marked as const.

Why undeclared identifier error occurres while I'm using objet in class's function?

The problem is that you're trying to access the fields brand, model and year on an object named carObj1 which isn't there in the context of the member function car::enterObj.

To solve this you can either remove the name carObj1 so that the implicit this pointer can be used or you can explicitly use the this pointer as shown below:

void enterCar() {
//-------------------------------------------vvvvv-------------->equivalent to writing `this->brand`
std::cout << "\nYour car is:" << brand <<std::endl;
//-----------------------------------vvvvvv------------------->explicitly use this pointer
std::cout<< " MODEL:" << this->model << std::endl;
std::cout<<" BUILT-IN:" << this->year;

}

Also i would recommend learning C++ using a good C++ book.



Related Topics



Leave a reply



Submit