c++ - 指针数组 C++

标签 c++ class pointers

我想创建一个指针数组来保存该类中对象的地址,这样当我调用扫描器函数时,它会读取 pcode 并搜索具有相同 pcode 的对象。我声明数组错了吗?还是我误解了静态概念?还是别的?

无论如何我想必须发布整个代码

#include <string>
using namespace std;
class product{
    public:
        product();
        product(long&,string&);
        void setCode();
        void getCode(long);
        void static scanner();
        void const printer();
        static product *point[3];
        static int a;
    private:
        string pname;
        long pcode;

};/*
class PrepackedFood:public product{
    public:
        PrepackedFood(long&, string&,double);
    private:
        double uPrice;
};
class FreshFood:public product{
    public:
        FreshFood(long&,string&,double,double);
    private:
        double weight;
        double pricepk;

};*/


#include "product.h"
#include <iostream>
product::product(){pcode=0;pname="unknown";
point[a]= this;
a++;}
product::product(long& c,string&n){pcode=c;pname=n;
}
//void const product::printer(){cout<<getCode()}
void product::setCode(){ cout<<"enter product name\n  ";cin>>pname;
cout<<"enter product code _____\b\b\b\b\b";cout<<"\a";
cin>>pcode;cout<<endl;
cout<<pname<<endl;
cout<<pcode<<endl;
}

void product::getCode(long s){
    if ((*this).pcode=s){
    printer();
    }
}
void product::scanner(){
    long a;
    cout<<"SCANNING!\a_____\b\b\b\b\b";cin>>a;
    int i=0;
    while(i<3){
        if (point[i]->pcode==a){point[i]->printer();
        break;
        }
        i++;    
        //(i==3)?cout<<"try again\n":"\a";
        }
}
void const product::printer(){
    cout<<pname<<endl;
    cout<<pcode<<endl;

}



#include "product.h"
int main(){
    product a[3];
    int i=0;
    while(i<3){
    a[i].setCode();
    i++;
    }

    product::scanner();

    return 0;
}

我知道它可以更容易地完成,我只是在学习所以只想修复扫描仪功能。它不编译

1>product.obj:错误 LNK2001:未解析的外部符号“public:静态类产品 * * product::point”(?point@product@@2PAPAV1@A) 1>product.obj : error LNK2001: 未解析的外部符号“public: static int product::a” (?a@product@@2HA)

最佳答案

代码看起来一团糟。

链接器问题的解决方案是定义已声明的静态 point 成员:

product* product::point[3];

关于c++ - 指针数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13366555/

相关文章:

c++ - 在 MinGW 上使用 libffi 调用 Windows API

c++ - 为什么 test2 + test3 的运算符 << 不能重载?

c - 为什么下面的代码不起作用?

c - 如何使用 C 中的函数生成随机 float 数组

c - C 中的运算符优先级(指针算术)

c++: 将变量单位的 std::chrono 转换为秒

c++ - 是否可以在控制台中用 C 打印孟加拉语?

C# 未处理类型为“System.StackOverflowException”的异常

c++ - 使用Boost将JSON解析对象存储到 map C++

javascript - 类中的异步/等待 : unexpected token `this`