Declaring Pointers/Passing Data

This is a discussion on Declaring Pointers/Passing Data within the C++ Programming forums, part of the General Programming Boards category; Code: Stock* newStock; newStock= new Stock(SN,Dn,CS,US,UP,MS); newStock->Print(); Here im declaring a pointer of type Stock called newStock. Then sending the ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    25

    Declaring Pointers/Passing Data

    Code:
    Stock* newStock;
    newStock= new Stock(SN,Dn,CS,US,UP,MS);
    newStock->Print();
    Here im declaring a pointer of type Stock called newStock. Then sending the data to the stock class and saving it to my pointer.
    I run the print method here as a way of checking if all has gone well.

    I have used similiar ways of doing this in past and never had any problems. This seems strange to me.

    I get these errors...

    Error: mainapp.cpp(189,17):Undefined symbol 'newStock'
    Error: mainapp.cpp(190,21):Type name expected
    Error: mainapp.cpp(190,21):Statement missing ;


    So...

    1. Is what im doing a standard way of doing things? or am i doing something inherantly wrong?

    2. Why is it telling me its undefined when i am defining it in that very piece of code.

    I think its something regarding the 'newStock= new Stock(SN,Dn,CS,US,UP,MS);' that is causing the problems, especially the new. But im really stumped. Any ideas?

    The stock.h and stock.cpp can be supplied if needed but i doubt its that.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    looks ok. maybe you have a small typo a bit further up in the code or something.Did u include stock.h?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    Rest of the program works fine without this function but when its included it throws a fit.

    If its of any benefit here is the whole function.

    Code:
    void Application::DoAddStock()
    {
    eMenu.getOption();
    string SN;
    string Dn;
    double CS;
    float US;
    float UP;
    double MS;
    
    	cout<<"\nEnter Stock Number : ";
    	Get(SN, 8);
    	cout<<"\nEnter a Description of the Product : ";
    	Get(Dn, 30);
    	cout<<"\nEnter Initial Stock Level : ";
    	Get(CS, 6);
    	cout<<"\nEnter Unit Size : ";
    	Get(US, 6);
    	cout<<"\nEnter Unit Price : ";
    	Get(UP, 6);
    	cout<<"\nEnter Minimum Stock Level :";
    	Get(MS, 6);
    
    
    Stock* newStock;
    newStock =new Stock(SN,Dn,CS,US,UP,MS);
    newStock->Print();
    }

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    is Get defined?
    What will u do about your memory leak?
    You make a new object on the heap but you neither delete it nor keep the pointer to it so that you can delete it later. Memory leak!
    silly question but is stock.h included?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    stock.h is included, the memory leak isnt really an issue atm, but ill add a destructor later.

    Get is part of the <iostream.h> header. Not my work.

    Should my stock class have some reference to this 'new new Stock(SN,Dn,CS,US,UP,MS);' bit, apart from the obvious constructor, which btw doesnt mention 'new' anywhere.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Get is not a member of any iostream library ive ever used. I think u mean get()
    C++ is case sensitive.
    Adding a destructor will not cure your memory leak. u have to delete that object at some time therefore you cannot afford to lose the pointer to it.
    and the last bit no. all u are doing is calling the constructor and making an object on the heap whose address is stored in the pointer.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    Well Get() is working, get() however does not work.

    This isnt the problem im quite sure.

    Stock files below.

    Code:
    /////////////////////////
    //Ryan Pincher
    //Stock.cpp
    //
    //
    //////////////////////////
    
    #include <defs.h>
    #include <iostream.h>
    #include <conio.h>
    #include <cstring.h>
    #include "stock.h"
    
    Stock::Stock()
    {
    StockNumber=" ";
    Description=" ";
    CurrentStock=0;
    UnitSize=0;
    UnitPrice=0;
    MinStock=0;
    };
    
    Stock::Stock(string SN, string Dn, double CS, float US, float UP, double MS)
    {
    StockNumber=SN;
    Description=Dn;
    CurrentStock=CS;
    UnitSize=US;
    UnitPrice=UP;
    MinStock=MS;
    };
    
    void Stock::Print(ostream& strm)
    {
    
    	strm << "\nStock Number : " << StockNumber<< endl;
    	strm << "Description : " << Description << endl;
    	strm << "Current Stock : " << CurrentStock << endl;
    	strm << "Unit Size : " << UnitSize << endl;
    	strm << "Unit Price : " << UnitPrice << endl;
       strm << "Minimum Stock : " << MinStock << endl;
    }; // end Print()
    
    
    ostream& operator<<(ostream& strm, Stock& S)
    {
    	S.Print(strm);
    	return strm;
    };
    Code:
    ////////////////////////////////////////
    // Ryan Pincher
    // Stock.h
    //
    //////////////////////////////////////////
    
    #include <defs.h>
    #include <iostream.h>
    #include <conio.h>
    #include <cstring.h>
    
    #ifndef STOCK_H
    #define STOCK_H
    
    
    class Stock
    {
    
    		private:
          //Attributes
          string	StockNumber;
          string	Description;
          double	CurrentStock;
          float		UnitSize;
          float		UnitPrice;
          double	MinStock;
    
          public:
          //Methods
          Stock();
          Stock(string SN, string Dn, double CS, float US, float UP, double MS);
          virtual void Print(ostream& strm = cout);
          string	getStockNo();
          string	getDescription();
          double   getCurrStock();
          float    getUnitSize();
          float    getUnitPrice();
          double   getMinStock();
    
    
          friend ostream& operator<<(ostream& strm, Stock& S);
    
    };
    
    #endif

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    what compiler you using?
    There is no Get() in my iostreams library nor on any compiler ive seen.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    Im using borland 5.01, Get() works so im not concerned with the case of the text.

    Just getting this data to go back to the class.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    Check istream.h its included in iostream

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I have never seen Get() in iostream or iostream.h either. Have seen get(). get() shouldn't be used to get numerical values, use >> instead.

    in Stock.h you use string variables but never #include the header file for string class which is

    #include <string>

    Unfortunately,

    #include <cstring.h>

    wrong on two accounts. First cstring is the file for functions that manipulate CStyle strings (null terminated char arrays) not objects of the STL string class as you try to do. Second, you don't use the .h extension with the preprocessor directive, just cstring.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 01:22 PM
  5. Replies: 1
    Last Post: 07-31-2002, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21