user379888
user379888

Reputation:

Unable to remove missing ; before variable name error

#ifndef PRODUCTS_H
#define PRODUCTS_H
#include "Products.h"
class Products
{
protected:
    static int count;
    string name_;
    float cost_;
public:
    Products()  // default ctor
    {
        name_ = "";
        cost_ = 0.0f;
        count++;
    }
    Products(string name , float cost)  //parametorized ctor
    {
        name_ = name;
        cost_ = cost;
        count++;
    }
    Products(Products &p )
    {
        name_ = p -> name_;
        cost_ = p -> cost_;
    }

    ~Products()
    {}

    string getName()
    {
        return name_;
    }
     void setName(string name)
    {
        name_=name;
    }
    float getCost()
    {
    return cost_;
    }
    void setCost(float cost)
    {
        cost_=cost
    }
    float CalcTotal(Products *p_products)   //Not made yet!
    {
        float total=0.0f;
        for(int i = 0 ; i < count; i++)
        {
            total += p_products->cost_;
            p_products ++;
        }
        return total;

    }
    Products read()
    {
        Products size,*p_products;
        cout << "Enter Number of Items To Enter:";
        cin >> size;
        cout << endl;
        p_products = new Products[size];
        for(int i = 0 ; i < size; i++)
        {
            cout << "Enter Name:";
            cin >> p_products -> name_;
            cout << endl << "Enter Cost";
            cin >> p_products -> cost_;
            cout << endl;
            p_products ++;
        }
        return p_products;
    }
    void write(Products *p_products)
    {
        for(int i = 0 ; i < count ; i++,p_products++)
        {
            cout<<"Products Name:\t\t"<<p_products->name_<<endl;
            cout<<"Products Price:\t\t"<<p_products->cost_<<endl;
        }
    }
};
#endif

My source code is:

#include <iostream>
#include <string>
#include "Products.h"
using namespace std;
static int Products::count;//declaring static variable
int main()
{
    Products *p_products,temp;
    *p_products=temp.read();
    //temp.write();
    system("pause");
    delete[] Product;
    return 0;
}

But I am getting this error which I can not remove:

error C2146: syntax error : missing ';' before identifier 'name_'

Please help me out!Thanks

Upvotes: 0

Views: 355

Answers (6)

Loghorn
Loghorn

Reputation: 2807

in your include file, you must declare the string name_ as std::string name_

Upvotes: 2

Colen
Colen

Reputation: 13918

Try moving this line:

using namespace std;

Above the line where you #include "Products.h". But if you're using string in products.h, you should probably be including etc there instead. Also, I believe "using namespace std" is kinda frowned upon.

Upvotes: 2

bstamour
bstamour

Reputation: 7776

You should include the string header file in your first file. It looks like it's complaining that it doesn't know what a string is.

You need to add

#include <string>

and change the type of name_ to

std::string name_;

Upvotes: 3

Maxpm
Maxpm

Reputation: 25612

You're missing a semicolon in this function:

void setCost(float cost)
{
    cost_=cost
}

Upvotes: 1

peoro
peoro

Reputation: 26060

In Products &p, p is a reference to an object of type Products, it's not a pointer.

You have to use operator ., instead of operator -> in order to access reference fields:

Products(Products &p )
{
    name_ = p -> name_;
    cost_ = p -> cost_;
}

Upvotes: 2

chrisaycock
chrisaycock

Reputation: 37928

You need:

std::string name_;

Also, looks like a missing semicolon here:

void setCost(float cost)
{
    cost_=cost
}

Upvotes: 1

Related Questions