GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Apr-2004, 21:47
Mjkramer21's Avatar
Mjkramer21 Mjkramer21 is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: Orem, Ut
Posts: 36
Mjkramer21 is on a distinguished road

help with c++ object assignment


Hey, Ive got this small assignment due tomorrow where I have to pass objects to functions. The header file and implementation file for the class works fine. Its the driver thats giving me a headache. I can't figure out why Im getting this error:

Error E2277 C:\UVSC\CNS1250\projects\complexNumbers\main.cpp 37: Lvalue required in function main()

Here is the code causing the problem. I'll comment on the line number where the problem is:
CPP / C++ / C Code:
#include "complex.cpp";

#include <iostream>
using namespace::std;

int main()
{

	double real1;
	double imag1;
	double real2;
	double imag2;

	double sum;
	double dif;



	cout << "Enter the real part of the first complex number " << endl;
	cin >> real1;

	cout << "Enter the imaginary part of the first complex number " << endl;
	cin >> imag2;

	cout << "Enter the real part of the second complex number " << endl;
	cin >> real2;

	cout << "Enter the imaginary part of the second complex number " << endl;
	cin >> imag2;

	complex comOne(real1, imag1);
	complex comTwo(real2, imag2);

             complexed();
	complexed = comOne.addComplex(comTwo); //Line 37, error



}

I went and researched the cause of this error and found that it's supposed to be due to me trying to put a value into an array that can't go into an array. The thing is, however, that I don't have an array anywhere in my code!
Last edited by Mjkramer21 : 10-Apr-2004 at 22:15. Reason: Please enclose c code in [c] & [/c] for syntax highlighting
  #2  
Old 11-Apr-2004, 00:50
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Is complexed supposed to be of type complex? All you have is the word complexed(); by itself in the middle of indentation land. The LValue error usually pops up when you're doing something with a structured data type, such as a class. Also, the return value of addComplex must be exactly the same type as the variable it's being assigned to in order for aggregate assignment to work. The problem appears to be with complexed().
__________________
-Aaron
  #3  
Old 11-Apr-2004, 01:33
Mjkramer21's Avatar
Mjkramer21 Mjkramer21 is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: Orem, Ut
Posts: 36
Mjkramer21 is on a distinguished road

problem fixed


yea, I found it .

I changed it from:
CPP / C++ / C Code:
  complexed();
  complexed = comOne.addComplex(comTwo); //Line 37, error

to:
CPP / C++ / C Code:
complex complexed;
complexed = comOne.addComplex(comTwo);

Duh! I still can't come up with the same values my instructor came up with. Would anybody be willing to look at my code for me and cross examine it with the example? Its 3 weeks until the end of the semester and this is the first program I've had a problem with. I think maybe Im just having a bad day. Maybe I should sleep on it.

Thanks for your feedback.
  #4  
Old 11-Apr-2004, 02:11
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
You probably just goofed one of the formulas or didn't use parenthesis well enough to clarify operator precedence... I'd be willing to look over your code, though, if you feel that you need help. I wrote the same program using regular structured data-types instead of classes. You can have a look at that if you'd like. The coding is very clear and easy to follow, and the formulas are tested and correct.
__________________
-Aaron
  #5  
Old 11-Apr-2004, 02:20
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
CPP / C++ / C Code:
struct complexNum
{
  float a;
  float b;
};

complexNum complexAdd(complexNum num1, complexNum num2);
complexNum complexSubtract(complexNum num1, complexNum num2);
complexNum complexMultiply(complexNum num1, complexNum num2);
complexNum complexDivide(complexNum num1, complexNum num2);
float complexAbs(complexNum num1);

complexNum complexAdd(complexNum num1, complexNum num2)
{
  complexNum result;

  result.a = (num1.a) + (num2.a);
  result.b = (num1.b) + (num2.b);

  return result;
}
//---------------------------------------------------------------------------
complexNum complexSubtract(complexNum num1, complexNum num2)
{
  complexNum result;

  result.a = (num1.a) - (num2.a);
  result.b = (num1.b) - (num2.b);

  return result;
}
//---------------------------------------------------------------------------
complexNum complexMultiply(complexNum num1, complexNum num2)
{
  complexNum result;

  result.a = ((num1.a) * (num2.a)) - ((num1.b) * (num2.b));
  result.b = ((num1.a) * (num2.b)) + ((num1.b) * (num2.a));

  return result;
}
//---------------------------------------------------------------------------
complexNum complexDivide(complexNum num1, complexNum num2)
{
  complexNum result;

  result.a = ((num1.a) * (num2.a)) + ((num1.b) * (num2.b));
  result.a /= ((num2.a) * (num2.a)) + ((num2.b) * (num2.b));
  result.b = ((num1.b) * (num2.a)) - ((num1.a) * (num2.b));
  result.b /= ((num2.a) * (num2.a)) + ((num2.b) * (num2.b));

  return result;
}
//---------------------------------------------------------------------------
float complexAbs(complexNum num)
{
  float result;

  result = ((num.a) * (num.a)) + ((num.b) * (num.b));
  result = sqrt(result);

  return result;
}
There's no main() function, because this program was designed for Windows using the Borland C++ Builder RAD. While the tool allows one to write a spiffy looking program for Windows in no-time, I don't think it lets you add your own windows API code, which kind of sucks, but oh well. If you still have questions, just ask and ye shall receive an answer.
__________________
-Aaron
  #6  
Old 11-Apr-2004, 11:39
Mjkramer21's Avatar
Mjkramer21 Mjkramer21 is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: Orem, Ut
Posts: 36
Mjkramer21 is on a distinguished road
Well that would be great if I could use structures. But I can't and I have to have a main as well. You can view the instructions here:

debryro.uvsc.edu.

I will attach my files incase your offer to look at my program is still open. Everything up until main is the way its supposed to be. Im just having a problem with the logic in main. I keep working it through in my head but keep coming up with the same thing. The program is adding the real numbers in both sets but not the imaginary for some reason, which doesnt makes sense because the addition of the real and imaginary takes place in the same function, one right after the other.
Attached Files
File Type: zip proj08mjk.zip (69.6 KB, 12 views)
  #7  
Old 11-Apr-2004, 11:56
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
When you take your input for the first imaginary number, you're accidentally taking the input into imag2 instead of imag1. This is most likely causing your problem.
__________________
-Aaron
  #8  
Old 11-Apr-2004, 12:29
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I edited the bajesus out of your program. I pretty much just changed all the doubles to ints, because you're probably never going to take a double for a complex number. Also, I added a print function to your class, which it should have already had. Finally, I changed your include and define statements, which all had semi-colons at the end of them... where the heck did you pick that habit up?! You also should use ".h" at the end of all your system headers instead of just the name of the file... you were saying <iostream>, which does actually exist in most compilers, but it's dumb to do that because it's the same as <iostream.h>, and it's better to be in the habit of using ".h" at the end of all of your includes.
Attached Files
File Type: zip proj08mjk (aaroncohn edit).zip (37.8 KB, 16 views)
__________________
-Aaron
  #9  
Old 11-Apr-2004, 12:51
Mjkramer21's Avatar
Mjkramer21 Mjkramer21 is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: Orem, Ut
Posts: 36
Mjkramer21 is on a distinguished road
I don't know, thats just what I was told to do. When I got my associates degree at a small tech school they told me to use .h but here at this 4 year college they said I don't have to do that anymore. Like you said most compilers don't care these days.

You obviously know what your doing though. Yea, that program isn't nearly complete yet. I have to clean it up, add comments, and print the results from a funtion. They are strict when it comes to style and all. At this point I was just trying to figure the logic with subtracting and adding the objects.

Did you ever figure out why it wasnt adding or subtracting the imaginary numbers instead of just the real numbers?
  #10  
Old 11-Apr-2004, 13:24
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Well, there was nothing wrong with your functions, if that's what you mean. You simply had used imag2 instead of imag1 while taking user input so that imag1 was never initialized and therefore contained garbage data. I didn't change the way your functions worked. I just changed the data type from double to int, and I added a print function, and I printed a small report in main(). I cleaned up your define and include statements, as well. Also, I didn't mean to say that most compilers don't care if you use the .h or not, because they do care. The thing is that most compilers come with an iostream file that doesn't have a .h at the end of it, so you're allowed to do that. If you try to do something like, #include <stdio>, you'll probably get a file-does-not-exist error.
__________________
-Aaron
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer values changing unexpectedly spudtheimpaler C Programming Language 11 04-Mar-2004 16:37
gxx linker accepts only 7 object files danielxs66 C++ Forum 1 12-Dec-2003 09:27
Create Vector To Hold Object? mukcl C++ Forum 3 15-Oct-2003 09:03
need help to define a class in C++ yannoush C++ Forum 7 09-Sep-2003 00:28
How do I access an object from inside another object? JdS MySQL / PHP Forum 5 10-Jun-2003 08:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.