开发者

C++ pointer error

开发者 https://www.devze.com 2023-02-07 14:08 出处:网络
When I tried to compile the following C++ program: //Source: C++ How To Program, Sixth Edition #include <iostream>

When I tried to compile the following C++ program:

//Source: C++ How To Program, Sixth Edition
#include <iostream>
int main()
{
int a;
int *aPtr;
a=7;
aPtr=&a;
std::cout<<"The address of a is: "<<&a<<std::endl;
std::cout<<"The value of aPtr is: "<<aPtr<<std::endl;
std::cout<<"The value of a is: "<<a<<std::endl;
std::cout<<"The value of *aPtr is: "<<*aPtr<<std::endl;
std::cout<<"Showing that * and & 开发者_JS百科are inverses of each"
<<" other"<<std::endl;
std::cout<<"&*aPtr= "<<&*aPtr<<std::endl;
std::cout<<"*&aPtr= "<<*&aPtr<std::endl;
return 0;
}

I got the following error:

C++ pointer error

Any ideas on that?

Thanks.


Replace

std::cout<<"*&aPtr= "<<*&aPtr<std::endl;

by

std::cout<<"*&aPtr= "<<*&aPtr<<std::endl;

Just a syntax error in your code ( < instead of << ).


It looks like a simple typo in line 15. You forgot one "<" between aPTR and the endl-constant ;)

std::cout<<"*&aPtr= "<<*&aPtr<std::endl;


You missed a < on the last line:

//----------------------------v here.
std::cout<<"*&aPtr= "<<*&aPtr<<std::endl;


Yes, the typo on line 15, where you've written < instead of <<. The error message makes that pretty clear!


Line 15 says *&aPtr < std::endl. should be << instead of <.

You would spot this kind of error more easily if you put spaces between operators and operants.


fix this line (syntax error) (<< instead of <)

std::cout<<"*&aPtr= "<<*&aPtr<std::endl;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号