无法编译以下示例代码(人指针) [英] cannot compile the following example code (person-pointer)
问题描述
我不明白这些错误:
g ++ Persort.cpp
Persort.cpp:在函数''int main()''中:
Persort.cpp:43:错误:''j'的名称查找已更改为新的ISO''for''范围
Persort.cpp:37:错误:在''j'使用过时的绑定'
- - - - -
#include< ; iostream>
#include< string> //对于字符串类
使用命名空间std;
///////////////////////// ///////////////////////////////////////
上课的人/ /一类人
{
受保护:
字符串名称; //人的姓名
public:
void setName()//设置名称
{cout<< 输入姓名:; cin>>名称; }
void printName()//显示名称
{cout<< endl<<名称; }
string getName()//返回名称
{返回名称; }
};
/////////////////////////////// /////////////////////////////////
int main()
{
void bsort(person **,int); //原型
person * persPtr [100]; //指向人的指针数组
int n = 0; //数组中的人数
char choice; //输入字符
做{//把人放在阵列中
persPtr [n] =新人; //创建新对象
persPtr [n] - > setName(); //设置人的姓名
n ++; //数新人
cout<< 输入另一个(是/否)?英寸; //输入另一个
cin>>选择; //人物?
}
while(choice ==''y''); //退出''n''
cout<< " \\\
nnsorted list:" ;;
for(int j = 0; j< n; j ++)//打印未排序的列表
{persPtr [j] - > printName(); }
bsort(persPtr,n); //排序指针
cout<< " \ nSorted list:" ;;
for(j = 0; j< n; j ++)//打印排序列表
{persPtr [j] - > ; printName(); }
cout<< endl;
返回0;
} //结束main()
// ------------ --------------------------------------------------
void bsort(person ** pp,int n)//对人的指示
{
无效订单(人**,人) **); //原型
int j,k; //索引到数组
for(j = 0; j< n-1; j ++)//外循环
for(k = j + 1 ; k< n; k ++)//内部循环从外部开始
顺序(pp + j,pp + k); //订购指针内容
}
// ------------------------- -------------------------------------
无效订单(人* * pp1,person ** pp2)//订购两个指针
{//如果第一个大于第二个,
if((* pp1) - > getName() >(* pp2) - > getName())
{
person * tempptr = * pp1; //交换指针
* pp1 = * pp2;
* pp2 = tempptr;
}
}
- - - - -
如何修复它们?
使用visual studio,我可以去msdn.com查看各种各样的错误。对于g ++有没有类似的东西?
祝你好运/ Med venlig hilsen
Martin J?rgensen
-
-------------------------------------------- -------------------------------
Martin J?rgensen的故乡 - http://www.martinjoergensen.dk
推荐答案
Martin J?rgensen写道:
Martin J?rgensen wrote:
Persort.cpp:在函数''int main()''中:
Persort.cpp:43:错误:''j'的名称查找已更改为新ISO''for'
范围
Persort.cpp:37:错误:在''j'使用过时绑定'
下次将错误信息的部分内容复制到Google中 - 它应该很容易找到这个.b / b
for(int j = 0; j< n ; j ++)//打印未排序的列表
.... for(j = 0; j< n; j ++)//打印排序列表
Persort.cpp: In function ''int main()'':
Persort.cpp:43: error: name lookup of ''j'' changed for new ISO ''for''
scoping
Persort.cpp:37: error: using obsolete binding at ''j''
Next time copy parts of the error message into Google - it should easily
find this one.
for(int j=0; j<n; j++) //print unsorted list .... for(j=0; j<n; j++) //print sorted list
ISO C ++标准改变了''j'的范围。它以前住在围绕第一个for循环的
块中。现在它只存在于这个for循环及其
控制的语句或块中。
第二个for循环现在看不到''j''。而不仅仅是未知
标识符或者其他什么,g ++维护者拦截了这个特殊的
情况,并给你一个完整的错误信息,描述新旧编译器行为之间的区别
。
-
Phlip
http ://www.greencheese.org/ZeekLand < - 不是博客!!!
The ISO C++ Standard changed the scope of ''j''. It formerly lived in the
block around the first for loop. Now it lives only in this for-loop and its
controlled statement or block.
The second for loop now sees no ''j''. Instead of just barfing "unknown
identifier" or something, the g++ maintainers intercepted this particular
situation and gave you a complete error message describing the difference
between the old and new compiler behavior.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
2006年3月25日星期六17:37:20 + 0100,Martin J?rgensen
< un ********* @ spam.jay.net>写道:
On Sat, 25 Mar 2006 17:37:20 +0100, Martin J?rgensen
<un*********@spam.jay.net> wrote:
我不明白这些错误:
g ++ Persort.cpp > Persort.cpp:在函数''int main()''中:
Persort.cpp:43:错误:''j'的名称查找已更改为新的ISO''为''范围确定
Persort.cpp:37:错误:在''j'使用过时绑定
j仅在第一个for的范围内环。在循环之外将其声明为循环或在第二循环中重新声明它。
[snip] for(int j = 0; j< n; j ++) //打印未排序的列表
{persPtr [j] - > printName(); }
j现在超出了范围。
bsort(persPtr,n); //排序指针
cout<< " \ nSorted list:" ;;
for(j = 0; j< n; j ++)//打印排序列表
for(int j = 0; //等
{persPtr [j] - > printName();}
cout<< endl;
返回0;
} //结束main()
// ---------------------------------------- ----------------------
void bsort(person ** pp,int n)//对人的指针
{
void order(person **,person **); // prototype
int j,k; //索引到数组
这些工作正常,因为两个计数器都是循环外可见
范围。
for(j = 0; j< n-1; j ++)//外循环
for(k = j + 1; k< n; k ++)//内部循环从外部开始
顺序(pp + j,pp + k); //命令指针内容
}
// --- -------------------------------------------------- ---------
无效顺序(人** pp1,人** pp2)//命令两个指针
{ //如果1st大于2nd,
if((* pp1) - > getName()> (* pp2) - > getName())
{
person * tempptr = * pp1; //交换指针
* pp1 = * pp2;
* pp2 = tempptr;
}
- - - - -
如何修复它们?
使用visual studio,我可以访问msdn.com并查找各种不同的错误。是否有像g ++这样的东西?
Hi,
I don''t understand these errors I get:
g++ Persort.cpp
Persort.cpp: In function ''int main()'':
Persort.cpp:43: error: name lookup of ''j'' changed for new ISO ''for'' scoping
Persort.cpp:37: error: using obsolete binding at ''j''
j is only in the scope of the first "for" loop. Declare it outside of
the loop or redeclare it within the second loop.
[snip] for(int j=0; j<n; j++) //print unsorted list
{ persPtr[j]->printName(); }
j is now out of scope.
bsort(persPtr, n); //sort pointers
cout << "\nSorted list:";
for(j=0; j<n; j++) //print sorted list
for (int j=0; // etc.
{ persPtr[j]->printName(); }
cout << endl;
return 0;
} //end main()
//--------------------------------------------------------------
void bsort(person** pp, int n) //sort pointers to persons
{
void order(person**, person**); //prototype
int j, k; //indexes to array
These work OK because both counters are visible outside the loop
scopes.
for(j=0; j<n-1; j++) //outer loop
for(k=j+1; k<n; k++) //inner loop starts at outer
order(pp+j, pp+k); //order the pointer contents
}
//--------------------------------------------------------------
void order(person** pp1, person** pp2) //orders two pointers
{ //if 1st larger than 2nd,
if( (*pp1)->getName() > (*pp2)->getName() )
{
person* tempptr = *pp1; //swap the pointers
*pp1 = *pp2;
*pp2 = tempptr;
}
}
- - - - -
How to fix them?
With visual studio, I could go to msdn.com and look up all kinds of
different errors. Is there something like that for g++?
你试过吗:
man gcc
或者:
男人g ++
?
-
Bob Hairgrove
No**********@Home.com
* Martin J?rgensen:
* Martin J?rgensen:
我不明白这些错误:
g ++ Persort .cpp
Persort.cpp:在函数''int main()''中:
Persort.cpp:43:错误:''j'的名称查找已更改为新的ISO''for''范围
Persort.cpp:37:错误:在''j'使用过时的绑定'
- - - - -
int main()
{
for(int j = 0; j< n; j ++)//打印未排序的列表
{persPtr [j] - > printName(); }
bsort(persPtr,n); //排序指针
cout<< " \ nSorted list:" ;;
for(j = 0; j< n; j ++)//打印排序列表
{persPtr [j] - > printName(); }
这里你需要写''for(int j = 0; ...'',以前的
声明j的范围结束了在for循环结束时。
如何修复它们?
I don''t understand these errors I get:
g++ Persort.cpp
Persort.cpp: In function ''int main()'':
Persort.cpp:43: error: name lookup of ''j'' changed for new ISO ''for'' scoping
Persort.cpp:37: error: using obsolete binding at ''j''
- - - - -
int main()
{
for(int j=0; j<n; j++) //print unsorted list
{ persPtr[j]->printName(); }
bsort(persPtr, n); //sort pointers
cout << "\nSorted list:";
for(j=0; j<n; j++) //print sorted list
{ persPtr[j]->printName(); }
Here you need to write ''for( int j=0; ...'', the scope of the previous
declaration of j ended at the end of that for-loop.
How to fix them?
为什么不用C ++实现程序C?< g>
例如,完全保留程序逻辑(或我能看到的最佳),
#include< algorithm> / / std :: sort
#include< iostream> // std :: cin,std :: cout
#include< istream> // operator>>
#include< ostream> // operator<< ;,std :: endl
#include< string> // std :: string
#include< vector> // std :: vector
class Person
{
protected:
std :: string myName;
public:
Person(std :: string const& name):myName(name){}
std :: string name()const {return myName;}
};
std :: string lineFrom(std :: istream&流)
{
std :: string line;
std :: getline(stream,line);
返回线;
}
std :: string nameFromUser()
{
std :: cout<< 输入姓名:; return lineFrom(std :: cin);
}
bool userAffirms(std :: string const& question)
{
字符选择;
std :: cout<<问题;
std :: cin>>选择;
std :: cin.ignore(INT_MAX,''\ n'');
return(choice!=''n''); //可以真正改进。
}
void printNameOf(Person const& person)
{
//保留前面的原始代码'换行符,但为什么选择它?
std :: cout<< std :: endl<< person.name();
}
void printList(std :: vector< Person> const& persons)
{
for(std :: size_t i = 0; i< persons.size(); ++ i)
{
printNameOf( person [i]);
}
}
bool order(Person const& a,Person const& b)
{
返回a.name()< b.name();
}
int main()
{
std: :矢量<人>人物;
do
{
persons.push_back(nameFromUser());
}
while(userAffirms(" Enter another(y / n)?"));
std :: cout<< \\\
nnsorted list:" ;; printList(人);
std :: sort(persons.begin(),persons.end(),order);
std :: cout<< \ nSorted list:" ;; printList(人);
std :: cout<< std :: endl;
}
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:什么是最烦人的事情usenet和电子邮件?
Why not implement the program in C++ instead of C? <g>
E.g., preserving the program logic exactly (or as best I could see),
#include <algorithm> // std::sort
#include <iostream> // std::cin, std::cout
#include <istream> // operator>>
#include <ostream> // operator<<, std::endl
#include <string> // std::string
#include <vector> // std::vector
class Person
{
protected:
std::string myName;
public:
Person( std::string const& name ): myName( name ) {}
std::string name() const { return myName; }
};
std::string lineFrom( std::istream& stream )
{
std::string line;
std::getline( stream, line );
return line;
}
std::string nameFromUser()
{
std::cout << "Enter name: "; return lineFrom( std::cin );
}
bool userAffirms( std::string const& question )
{
char choice;
std::cout << question;
std::cin >> choice;
std::cin.ignore( INT_MAX, ''\n'' );
return (choice != ''n''); // Can be Really Improved.
}
void printNameOf( Person const& person )
{
// Original code''s newline at front preserved, but why choose that?
std::cout << std::endl << person.name();
}
void printList( std::vector<Person> const& persons )
{
for( std::size_t i=0; i < persons.size(); ++i )
{
printNameOf( persons[i] );
}
}
bool order( Person const& a, Person const& b )
{
return a.name() < b.name();
}
int main()
{
std::vector<Person> persons;
do
{
persons.push_back( nameFromUser() );
}
while( userAffirms( "Enter another (y/n)? " ) );
std::cout << "\nUnsorted list:"; printList( persons );
std::sort( persons.begin(), persons.end(), order );
std::cout << "\nSorted list:"; printList( persons );
std::cout << std::endl;
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
这篇关于无法编译以下示例代码(人指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!