结构体数组赋值

C++语言 码拜 7年前 (2017-05-04) 1290次浏览
本人

#include <iostream>
using namespace std;
struct NoCutterspace
{
	double NoWidth;
	double NoPolar;
	double NoAngle;
}no[]={
		{1,2,1},
		{2,4,6},
		{6,6,8},
		{4,5,6}
	};
printfPolar()
{

	int NoNum=sizeof (no)/(sizeof (double)*3);
	cout<<no[NoNum-1].NoPolar<<endl;
}
int main()
{
    printfPolar();
	return 0;
}

上面的在main外赋值结构体数组可以,但是本人希望在main里面赋值结构体数组,并且在自定义函数printfPolar里对这个要输入的数组进行一些操作,在main函数里赋值就会报错,这样就不行了:

#include <iostream>
using namespace std;
struct NoCutterspace
{
	double NoWidth;
	double NoPolar;
	double NoAngle;
}no[];
void printfPolar()
{
    struct NoCutterspace no[];
	int NoNum=sizeof (no)/(sizeof (double)*3);
	cout<<no[NoNum-1].NoPolar<<endl;
}
int main()
{
 	NoCutterspace no[]={
		{1,2,1},
 		{2,4,6},
		{6,6,8},
 		{4,5,6}
 	};
   printfPolar();
	return 0;
}

讨教大家,上面程序怎么改呢

解决方案

40

引用:
Quote: 引用:
#include <iostream>
using namespace std;
 
struct NoCutterspace
{
    double NoWidth;
    double NoPolar;
    double NoAngle;
};
 
void printfPolar(NoCutterspace *p, int num)
{
    //struct NoCutterspace no[];
    //int NoNum=sizeof (no)/(sizeof (double)*3);
    //cout<<no[NoNum-1].NoPolar<<endl;
    cout<<no[num-1].NoPolar<<endl;
}
 
int main()
{
 
     NoCutterspace no[]={
        {1,2,1},
         {2,4,6},
        {6,6,8},
         {4,5,6}
     };
   printfPolar(no, 4);
 
    return 0;
}

这样还是不行:
error C2065: “no” : undeclared identifier
error C2109: subscript requires array or pointer type
error C2228: left of “.NoPolar” must have class/struct/union type

本人的错,没把名字修正过来
你把printfPolar里面的no改为p


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明结构体数组赋值
喜欢 (0)
[1034331897@qq.com]
分享 (0)