Hi everybody
Thanks again for your helps in my last thread.

Now, I have some more problems. :(

50 Errors for this program. Certainly, a basic problem within the structure of class "Block".

This assignment is all about "Class and objects". We can use functions, but no arrays.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

ofstream outfile ("lab8.out");
ifstream infile ("lab8.in.text");


class Block
{ 
public:
	double weight, friction, force;
	};

int main ()

{
  	

infile>>friction>>weight>>angle1>>angle2>>angle3>>angle4>>angle5>>angle6>>angle7>>angle8>>angle9;

Block angle1, angle2, angle3, angle4, angle5, angle6, angle7, angle8, angle9;
force.angle1==(weight-(weight*friction))/cos(angle1);
force.angle2==(weight-(weight*friction))/cos(angle2);
force.angle3==(weight-(weight*friction))/cos(angle3);
force.angle4==(weight-(weight*friction))/cos(angle4);
force.angle5==(weight-(weight*friction))/cos(angle5);
force.angle6==(weight-(weight*friction))/cos(angle6);
force.angle7==(weight-(weight*friction))/cos(angle7);
force.angle8==(weight-(weight*friction))/cos(angle8);
force.angle9==(weight-(weight*friction))/cos(angle9);

outfile<<"Angle (Degrees)\t"<<"Force, F(KN)"<<endl
       <<"---------------\t"<<"------------"<<endl;
cout<<"Angle (Degrees)\t"<<"Force, F(KN)"<<endl
    <<"---------------\t"<<"------------"<<endl;

outfile<<setw(5)<<setfill(' ')<<angle1<<setw(6)<<force.angle1<<endl
       <<setw(5)<<setfill(' ')<<angle2<<setw(6)<<force.angle2<<endl
	   <<setw(5)<<setfill(' ')<<angle3<<setw(6)<<force.angle3<<endl
	   <<setw(5)<<setfill(' ')<<angle4<<setw(6)<<force.angle4<<endl
	   <<setw(5)<<setfill(' ')<<angle5<<setw(6)<<force.angle5<<endl
	   <<setw(5)<<setfill(' ')<<angle6<<setw(6)<<force.angle6<<endl
	   <<setw(5)<<setfill(' ')<<angle7<<setw(6)<<force.angle7<<endl
	   <<setw(5)<<setfill(' ')<<angle8<<setw(6)<<force.angle8<<endl
	   <<setw(5)<<setfill(' ')<<angle9<<setw(6)<<force.angle9<<endl<<endl;
       
cout<<setw(5)<<setfill(' ')<<angle1<<setw(6)<<force.angle1<<endl
       <<setw(5)<<setfill(' ')<<angle2<<setw(6)<<force.angle2<<endl
	   <<setw(5)<<setfill(' ')<<angle3<<setw(6)<<force.angle3<<endl
	   <<setw(5)<<setfill(' ')<<angle4<<setw(6)<<force.angle4<<endl
	   <<setw(5)<<setfill(' ')<<angle5<<setw(6)<<force.angle5<<endl
	   <<setw(5)<<setfill(' ')<<angle6<<setw(6)<<force.angle6<<endl
	   <<setw(5)<<setfill(' ')<<angle7<<setw(6)<<force.angle7<<endl
	   <<setw(5)<<setfill(' ')<<angle8<<setw(6)<<force.angle8<<endl
	   <<setw(5)<<setfill(' ')<<angle9<<setw(6)<<force.angle9<<endl<<endl;

infile.close();
outfile.close();
return 0;
}

Recommended Answers

All 19 Replies

You'll need to read the chapter on classes and objects again. You clearly didn't understand it sufficiently to write any code. Once you figure out the relationship between objects and members, you'll be able to clear up the majority of those errors.

Try cutting down your program to just a few lines,
check the compile errors, fix those.
Then you should understand what you did wrong.

I see assignment errors..:p

Then show us the line that contains the assignment error.
And maybe even the compile error itself.

And in addition to what Narue already said, I would like to add that you probably don't want do declare your file-streams in a global-scope. In this case (and in most cases) there is absolutely no reason to do this, so just declare (and open) them in main() (in your case)

OK. Here we go. The most confusing one for me is:

error C2065: 'friction' : undeclared identifier

Same for weight, force, angle1 to angle 9.

And some more:

error C2228: left of '.angle1' must have class/struct/union type

Same for angle2 to angle9.

I simply don't get the meaning. "left of ....", so what?

Dozens of this error as well:

error C2664: 'cos' : cannot convert parameter 1 from 'class Block' to 'double'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

For the first line of outfile/cout:

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Block' (or there is no acceptable conversion)

Who..Me or OP..:p..Nevermind..:p

Who..Me or OP..:p..Nevermind..:p

Any of you, maybe. :)

error C2065: 'friction' : undeclared identifier

means there is not free standing variable called friction. Yet, on line 23 you write code suggested that's what you want. That compiler is saying you can't do that. friction is a member variable of the Block class, but to do anything with it you have to first declare a variable of type Block and then access the desired member variable using the dot operator, or the arrow operator if you are refering to a pointer to a Block object.

Yes, you need to read the chapter on classes again. There are several easily fixable errors here. Your member variables for starters should not be declared public unless you need them to be modified by non class members. Where are your function prototypes or definitions for member functions? Force I'm guessing is a function? The error you are receiving...
"error C2228: left of '.angle1' must have class/struct/union type"
means that the member variable that is left of .angle must be a class struct or union object but you have it defined as a member of the block class which you then call before the instance object of that class, this makes no sense. You need to go back and read the basic use of the dot operator over and how to correctly define class members. Don't regurgitate code your instructor gives and try to piece together your own solution from it. Understand what the problem is asking of you first, write it down in spoken language first. Then shoot out the code.

The scope for friction is wrong somewhere, without knowing what else your project entails it's hard to know where the error is resulting from. But essentially you are using a variable that is outside of the scope of where it is defined. The other error about conversion means that you need to cast which is probably a bad idea or you need to use user defined overloaded operators. Which if you are this far into classes you should have learned about this already.

commented: Codes plz. :D >:) +1

Well, I think there should be a rule making helpers not come up with words only, but codes also. :)

The problem is actually asking to calculate a pulling for exerted by a rope on the block which is on a surface with friction. We should use a class named Block and if necessary, functions.

Is there any probable dispute/ confusion between the angle1 (for instance) in "ifstream" and class Block, because of the same name?

commented: "gimme codez" -3

>Well, I think there should be a rule making helpers
>not come up with words only, but codes also.
Oddly enough, there's a rule that requires us not to do so until you show sufficient effort to deserve replies with code. You see, the problem here is that your code is so completely wrong, it's obvious that you don't understand the first thing about classes or even basic C++. It's not worth our time to give you "codes" because you won't understand it anyway.

My original advice still stands. Go read your book again. It's immediately obvious that you lack the fundamental knowledge required to write your program when I look at code like this:

class Block {
public:
  double weight, friction, force;
};

Block angle1;

// WTF!?
force.angle1==(weight-(weight*friction))/cos(angle1);

You guys don't know how to help at all. :@

Here's the code and I'll explain myself for those who have the same problem.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

class Block
{
private : double angle;
              double f;

public  : 
double calc(double);

};

double Block::calc(double a)

{

 angle= a*((3.142)/180);

  f=(6000/( (cos (angle)) + (0.2*(sin (angle)))))/1000.0;
return f;

}


int main()

{

       double a;
       double f;
     ifstream infile("lab8.dat");
     ofstream outfile("lab8.out");

cout<<"Angle, (degrees)\t"<<"Force,F (kN)"<<endl
    <<"----------------\t"<<"------------"<<endl;
outfile<<"Angle, (degrees)\t"<<"Force,F (kN)"<<endl
       <<"----------------\t"<<"------------"<<endl;

for (int i=1; i<=9 ; i++)
{
       Block force;

       infile>>a;
      
	   f=force.calc(a);

	   cout<<setprecision(2)<<resetiosflags(ios::fixed|ios::showpoint)
	   <<setiosflags(ios::right);
 outfile<<setprecision(2)<<resetiosflags(ios::fixed|ios::showpoint)
	   <<setiosflags(ios::right);
       cout<<setw(3)<<a<<setw(32)<<setiosflags(ios::fixed|ios::showpoint)<<f<<endl;
       outfile<<setw(3)<<a<<setw(32)<<setiosflags(ios::fixed|ios::showpoint)<<f<<endl;

}
	  

outfile.close();
infile.close();	   

return 0;

}

Hints:
1) Usual variables are "private"; functions should be "public" (double type).
2) Create a joint class-function (double type) returning the expression component (Here f); use :: to join'em.
3) Note that C++ only understand Radian; therefore you need to convert the angles from degree to Radian.
4) cout/outfile and the loop containing the infile goes to the main function (int main ()). within the body of this loop, you should call a new object (here force, i.e. Block force; ) to call the returning component f as the object.function(dependent) i.e force.calc(a); dependent comes from the infile, most of the times. Also, you can directly call for force.calc(a) in cout/ outfile without defining the returning component (f) again.

>You guys don't know how to help at all.
Some people are beyond help. Your new code is actually syntactically correct, so either some kind soul fixed it for you or you were just screwing with us from the start.

>You guys don't know how to help at all.
Some people are beyond help. Your new code is actually syntactically correct, so either some kind soul fixed it for you or you were just screwing with us from the start.

You could help in a better way. I didn't know that function is a must. you have to have a function member. That was the biggest problem, I've faced. You must join the class and the function with ::.

example: double Block::calc(double a)

Secondly, an object (like force here) is for nothing, but only calling the final value(i.e. force.calc(a) where force has no definition, but is an object of class Block).

Block force; infile>>a; f=force.calc(a);

Thirdly, the angles should be expressed in Radian only.

angle= a*((3.142)/180);

This is what I call it help even for dummies.

>This is what I call it help even for dummies.
I boiled all of that down into a simple piece of advice: read your book. I'm generally hostile toward people who try to waste my time with questions that are easily answered by doing what they should have done in the first place.

I've already read the book and it didn’t prove useful, at least for me. That’s why I was here.

Besides, a book with 1000 pages is too much for 4 months. I cannot read it, line by line.
I’ve taken 16 credit hours for this semester (7 subjects) and the reference books all together are almost about 5000 pages.
And there’s an easier way, indeed. I come here and ask for help; others copy from each other.
Anyway, I appreciate your help, despite your bitter reactions sometimes.

Don't come asking for help when you can search your help all by yourself.

This is the first hit on goole c++ class example
http://functionx.com/cpp/examples/simpleclass.htm
And I think it covers your problems.

People are not angry with you,
but are annoyed by the fact that you didn't bother to check up on a solution for yourself.

If you think your book is bad,
buy another book.

If you are overburden by your courses,
skip one of them.

I have no idea what 16 credit hours mean,
nor do I know what 7 subjects for a semester mean.

And actually I don't care so much.


Feel free to ask for help again,
but atleast give google a try.

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.