Invalid use of class

This is a discussion on Invalid use of class within the C++ Programming forums, part of the General Programming Boards category; I have to create two classes CPU and Computer. The CPU.h and Computer.h needs to be different files than the ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    25

    Invalid use of class

    I have to create two classes CPU and Computer. The CPU.h and Computer.h needs to be different files than the CPU.cpp and Computer.cpp. I believe I created my files correctly however in my Computer constructor, I'm told to "allocates memory for the pointer of of CPU (i.e., call the constructor of the CPU class)." I'm not sure if I'm doing that right because I get the error 13 invalid use of `class CPU' , I bolded the line which it happens on

    Heres Computer.cpp:
    Code:
    //File Computer.cpp
    
    #include <string>
    #include<iostream>
    #include "CPU.h"
    #include "Computer.h"
    
    Computer:: Computer()
    {
              brandName = "?";
              memory = 0;
              price = 0.0;
              *cpu->CPU();
    }
    
    Computer:: ~Computer()
    {
               cout<<"The computer "<<brandName<<" is getting destroyed."<<endl;
               free(cpu);           
    }
    
    string Computer:: getBrandName()
    {
           return brandName;
    }
    
    CPU * Computer:: getCPU()
    {
           return cpu;
    }
    
    int Computer:: getMemory()
    {
           return memory;
    }
    
    double Computer:: getPrice()
    {
           return price;
    }
    
    void Computer:: setBrandName(string BrandName)
    {
         brandName = BrandName;
    }
    
    void Computer:: setCPU(string cpuType, int cpuSpeed)
    {
         cpu->setType(cpuType);
         cpu->setSpeed(cpuSpeed);
    }
    
    void Computer:: setMemory(int memoryAmount)
    {
         memory = memoryAmount;
    }
              
    void Computer:: setPrice(double price)
    {
         price = price;
    }
    
    void Computer:: printInfo()
    {
         cout<<"\nBrandName:\t"<<brandName<<"\nCPU:\t\t"<<cpu->getType()<<","
         <<cpu->getSpeed()<<"HZ\nMemory:\t\t"<<memory<<"M\nPrice:\t\t$"<<price
         <<"\n\n";
    }

    Heres Computer.h:
    Code:
    //File Computer.h
    
    #include <string>
    #include "CPU.h"
    //#include <iostream>
    using namespace std;
    #ifndef Computer_H
    
    #define Computer_H
    
    class Computer
    {
     private:
             string brandName;
             CPU * cpu;
             int memory;
             double price;
             
      public:
             Computer();
             ~Computer();
             string getBrandName();
             CPU* getCPU();
             int getMemory();
             double getPrice();
             void setBrandName(string);   
             void setCPU(string, int);   
             void setMemory(int);   
             void setPrice(double);
             void printInfo();
    };
    
    #endif
    Heres CPU.h:
    Code:
    // File CPU.h
    
    #include <string>
    using namespace std;
    #ifndef CPU_H
    
    #define CPU_H
    
    class CPU
    {
     private:
             string type;
             int speed;
     
     public:
            CPU();
            ~CPU();
            string getType();
            int getSpeed();
            void setType(string);
            void setSpeed(int);
            void printInfo();
            
    
    };
    
    #endif
    And heres CPU.cpp
    Code:
     // File CPU.cpp
    
    #include <string>
    #include <iostream>
    
    #include "CPU.h"
    
    
    CPU:: CPU()
    {
          type = "?";
          speed = 0;
    }
    
    CPU:: ~CPU() 
    {
           cout<<"CPU with the type "<< type <<" and the speed "<< speed <<" is being destroyed." <<endl;       
    }
    
    string CPU:: getType()
    {
            return type;
    }
    
    int CPU:: getSpeed()
    {
            return speed;
    }
            
    void CPU:: setType(string type)
    {
            type = type;
    }
    
    void CPU:: setSpeed(int speed)
    {
            speed = speed;
    }
    
    void CPU:: printInfo()
    {
            cout<<type<<","<<speed<<"HZ"<<endl; 
    }

    Any help would be appreciated

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, don't use 'free' (for now, anyway), as it won't call the destructor on the object. You should allocate with 'new' and deallocate with 'delete' (eventually, you'll need to get a firm grasp of RAII, but for now we'll just leave it that).

    Anyway, a pointer is just an address, and right now your 'cpu' variable points to nowhere in particular. Again, you'll need to use 'new' to allocate some memory for it, and then assign that directly to 'cpu'. The 'new' expression takes any valid arguments to the type's constructor, and for an object of type 'CPU', no arguments are needed. The syntax/idiom you should be using is similar to this:

    Code:
    std::string* s = new std::string( "I'm alive!" );
    // ...do something...
    delete s; // calls ~std::string() and releases memory

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Also, do not call the constructors or destructors directly unless you are sure what you are doing, and in this case, you are not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Are you guys in the same class?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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