Hello, and thank you for taking the time to help me! This is my first real program in C++, and I am still getting used to the differences between C and C++, so I still really don't know what I'm doing. I am working on a header file and its associated source code, and am getting two repetitive errors when I compile the source code. I get these errors for the source code for every line where there is a new function being defined. The errors are as follows:

CPU.cpp expected unqualified-id before "public"
CPU.cpp expected `,' or `;' before "public"

I have tried adding ; in multiple places, and I just can't figure out what this error is telling me. I am sure it is something very basic, but I am at a loss. Can someone help me with this?

Thank you in advance for any help you can provide!

Here are the two files:

// CPU.h

#ifndef CPU_H
#define CPU_H

using namespace std;

class CPU
{
      private:
        string type;
        int speed;
        
      public: 
        CPU();
        ~CPU();
        string getType();
        int getSpeed();
        void setType(string type);
        void setSpeed(int speed);
        void printInfo();
};

#endif
// CPU.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include "CPU.h"
using namespace std;


CPU::CPU()
{
       type = "?";
       speed = 0;
}
        
//CPU::~CPU()
//{
//       cout << "CPU with the type " << type << " and the speed " << speed << " is being destroyed." << endl;
//       if (type)
//          delete[] type;
//       if (speed)
//          delete[] speed;
//}
        
public string CPU::getType()
{
       return type;
}
        
public int CPU::getSpeed()
{
       return speed;
}
        
public void CPU::setType(string newType);
{      
       type = newType;
}
       
public void CPU::setSpeed(int newSpeed);
{
       speed = newSpeed;
}

public void CPU::printInfo();
{
       cout << type << "," << speed << endl;
}

When I create classes in c++ I include the header and all of the functions, constructors, etc in the same file. I'd call it CPU.cpp.
Then I create a new file with the main method with the following line:

#include "CPU.cpp"

Not sure if that is neccesary but maybe worth a shot.

In CPU.cpp, remove public from all of the method definitions. C++ does not work like Java and C#, where the access level is applied to each entity.

When I create classes in c++ I include the header and all of the functions, constructors, etc in the same file. I'd call it CPU.cpp.
Then I create a new file with the main method with the following line:

#include "CPU.cpp"

Not sure if that is neccesary but maybe worth a shot.

Thank you for the quick response!

Unfortunately, I can't do what you said, because this is for a homework assignment and I have to turn in separate header files and cpp files for each class. So I don't think that I can implement it this way. Unless I am misunderstanding the way that I am supposed to do this...which is entirely possible because I am so brand spanking new to this, heh heh. But I think I'm supposed to turn in separate files.

Thank you for the advice, though! I will utilize this strategy in the future if the requirements are different. And I'm sorry to be difficult!

In CPU.cpp, remove public from all of the method definitions. C++ does not work like Java and C#, where the access level is applied to each entity.

Wow! How did I miss that one. ;)
Another good call Tom Gunn. :)
@OP read Tom Gunn's response.

In CPU.cpp, remove public from all of the method definitions. C++ does not work like Java and C#, where the access level is applied to each entity.

Oh, okay...that makes sense. So removing those got rid of a bunch of the errors, but I am still getting them on some of the lines. Here is my updated CPU.cpp code:

// CPU.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include "CPU.h"
using namespace std;


CPU::CPU()
{
       type = "?";
       speed = 0;
}
        
//CPU::~CPU()
//{
//       cout << "CPU with the type " << type << " and the speed " << speed << " is being destroyed." << endl;
//       if (type)
//          delete[] type;
//       if (speed)
//          delete[] speed;
//}
        
string CPU::getType()
{
       return type;
}
        
int CPU::getSpeed()
{
       return speed;
}
        
void CPU::setType(string newType);
{      
       type = newType;
}
       
void CPU::setSpeed(int newSpeed);
{
       speed = newSpeed;
}

void CPU::printInfo();
{
       cout << type << "," << speed << endl;
}

And here are the errors that I am getting:
35 CPU.cpp declaration of `void CPU::setType(std::string)' outside of class is not definition
36 CPU.cpp expected unqualified-id before '{' token
36 CPU.cpp expected `,' or `;' before '{' token
40 CPU.cpp declaration of `void CPU::setSpeed(int)' outside of class is not definition
41 CPU.cpp expected unqualified-id before '{' token
41 CPU.cpp expected `,' or `;' before '{' token
45 CPU.cpp declaration of `void CPU::printInfo()' outside of class is not definition
46 CPU.cpp expected unqualified-id before '{' token
46 CPU.cpp expected `,' or `;' before '{' token

Your last few functions have semicolons after the function name. Get rid of 'em.
Guess I missed that too, originally. ;)

void CPU::setType(string newType);
{      
       type = newType;
}

Above is an example of the problem. Look for the extra semicolon after the parameter list. That should not be there either. Rinse and repeat for all of your method definitions. And be careful when copy/pasting declarations and turning them into definitions. I accidentally copy the semicolon a lot too. ;)

Oh, okay, I see the problem there! I took out those semicolons and it compiles fine now. Thank you guys for the help!! :)

Oh, okay, I see the problem there! I took out those semicolons and it compiles fine now. Thank you guys for the help!! :)

No problem, glad to help. :)
Can you please click "Mark as solved" at the bottom of the page if you have no further questions.
Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.