![]() |
|
#1
|
|||
|
|||
Point to Point3D Inheritance***EDIT***
I have the following code This is the Point header file CPP / C++ / C Code:
This is the cpp file defining the Point class: CPP / C++ / C Code:
This is the Point3D header file: CPP / C++ / C Code:
******This is the updated cpp file defining the Point3D class: CPP / C++ / C Code:
*****And this is the updated cpp file to test if it works: CPP / C++ / C Code:
I know that this bunch of files, but I am not allowed to combine them, since this is a test of my knowledge of inheritance. After I figure out what is wrong, I will have to finish the code. The program will have to take values from an input file, which will contain two or more points, and the program should take two of the points and find the distance between them. After that, the program will output the results in an output file. Again, this is not shown because I am not sure how to do that. I am getting the following errors:****FIXED**** asd error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Point3D const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@ std@@AAV01@ABVPoint3D@@@Z) already defined in Point3D.obj asd fatal error LNK1169: one or more multiply defined symbols found ***Now my problem is getting the program to read the input files and outting them into the output files. The test cpp file has the code for getting the input and output files to open and to put the stuff in the input and copying it to the output. Is there a way to do this, (opening input and output files) on Microsoft visual studio? Could someone please skim through the code and find what I am doing wrong? I would be grateful. Thank you. |
|
#2
|
|||
|
|||
Re: Point to Point3D InheritanceQuote:
The way that the names get munged makes it kind of hard to get anything useful from the message, so you have to try to see if any part of it has any recognizable stuff --- like picking the fly specks out of the pepper (and I apologize to anyone who is eating as they read this). The error indication seems to be telling us that there is some kind of operator: "<<" that is defined in such a way that it is ambiguous. I think it is telling us that it was defined in whatever file it was that gave you Point3D.obj (maybe the file name is Point3D.cpp, or some such thing). The other definition is in the file that was being linked after that one (maybe Point.cpp?) Anyhow, I would look for file(s) that have some kind of definition(s) for a "<<" operator. Are they correct? Regards, Dave |
|
#3
|
|||
|
|||
Re: Point to Point3D InheritanceTo clear things up:
I fixed my old problem, but I have a new problem with the program. I don't know how I would incorporate an input.txt file and an output.txt file. The program takes the numbers in the input.txt file and calculates the distance between the points. Then it would output it in the output.txt file. An example of the input.txt file would look like: A 3 4 5 B 3 4 5 (those numbers would correspond to the x, y, z coords) then the output would be: The distance between A and B is 0 |
|
#4
|
|||
|
|||
Re: Point to Point3D InheritanceQuote:
Here's a quick reference program using input and output fstreams. In a practical program, the file names would probably be obtained from user input (from command line arguments, or by prompting the user from the program). I just did the calculations by 'brute force', instead of using your neat classes. Your classes might include overloaded operators for >> (extraction of items from ifstreams) and << (insertion of data into ofstreams). But, my usual practice is to first of all, make sure that I can do it. Then I do it "nice and pretty". CPP / C++ / C Code:
My "input.txt" file looked like this: Code:
The program console output looked like this: Code:
After the program run, file out.txt looked like this: Code:
Notes: I always test to make sure files are opened properly and I test the state of the stream after obtaining user input data (from files or console). I always print out any data items that I obtained from user input (files or console). I usually print "status report" items as files are opened, and at other significant places in the flow. That way if things go bad, I have some indicaton of how far it got before the big bomb. Regards, Dave |
|
#5
|
|||
|
|||
Re: Point to Point3D InheritanceOk... I kind of get what you were saying and I modified the program a little. I have the input and output files working.
The thing is, I don't know how I would get the program to read the values in the input.txt file. |
|
#6
|
|||
|
|||
Re: Point to Point3D InheritanceQuote:
Use a container of points such that you add a Point3D to the container every time you read a 3D point from the file. Open your file, then use your streaming (insertion/extraction) operators to initialize your Point3D instances. Implement your operators "-", "+", ">", "<", "=", "==" and "!=" so that you can discern whether the points are different and if they are, the differences between them. You'll also want to implement a copy constructor and a destructor. Your use of inheritance suggests to me that Point should probably have some behavioral specification for derived types...and at least a virtual destructor. :davis: |
|
#7
|
|||
|
|||
Re: Point to Point3D InheritanceQuote:
My example read the values from test.txt (since that is the name of the file used to open the inFile ifstream). Quote:
For your project, you might create an overloaded ">>" operator in the class definition so that your program could simply do something like this: CPP / C++ / C Code:
Now, in my simple example, I just bailed out of the program at the first sign of trouble (it's only a demonstration, after all). In your class definitions, you would probably want to be a little more civilized and throw an exception (or something). That's your call (since it's your project, not mine). Regards, Dave |
|
#8
|
|||
|
|||
Re: Point to Point3D InheritanceQuote:
I am lost. What should I do first? |
|
#9
|
|||
|
|||
Re: Point to Point3D InheritanceQuote:
I can't possibly know what your requirements (get file name from user or use some predefined name, etc.) and project parameters (deadline, etc.) are. I also don't know what you are expected to use in order to complete the assignment (overloaded i/o operators or what?) Therefore, any guidelines that I could come up with would necessarily fall into the category of vague generalities. Here are a few vague generalities: If it were my project, I might take the basic class definitions, and in my main() program I would read input lines from a given file and put the name and numbers into the individual name and coordinate values of a couple of points (using the class member functions setName(), setX(), etc.). I would probably use getX(), etc., to retrieve the values from the objects and print out their values in main(). Then I would calculate the distance using the mathematical formula on the individual items. Then I would call whatever distance() function I had already defined to calculate the distance, and print out the result to make sure that it had the correct value. If I was supposed to use an overloaded input operator to read the values, I would implement this next. I would probably practice with a simpler class that only reads things into one member so that I could make sure that I knew how to implement an overloaded operator. Then I might try it on my 2Dclass that takes a string and two numbers. Then I might try it on my 3Dclass that takes a string ant three numbers. I would debug each by testing it by itself (and printing out the values that it read in each time) and go on to whatever other tasks that the assignment requires. If I had a problem with any particular task, I would break that part out into a separate test program that just did whatever one new thing that I was trying to accomplish and make sure that I really nailed it before trying to integrate it back into the main flow. Regards, Dave |
|
#10
|
|||
|
|||
Re: Point to Point3D InheritanceQuote:
This is perfect. The sentence in bold is where I am having trouble. Requirements: The teacher is using UNIX to compile the program, so when she executes the file, she will have to execute the file and the input and output txt files. ex: % point3d-test input.txt output.txt The input.txt file is someone she will make up. It will consist of two lines : A 4 3 2 B 5 5 3 for example. The output file will be created after the program is run and it will contain the distance between the points in the input.txt file. We are to have an overloaded << operator. I have most of it in the program already. Due wednesday at 3pm. Other than that, everything is up for grabs. Nothing too complex. |
Recent GIDBlog
Developing GUIs with wxPython (Part 3) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c++ inheritance | illbemissingu | C++ Forum | 14 | 23-Oct-2005 17:35 |
| Inheritance Project .. | Qatar | C++ Forum | 1 | 28-Apr-2005 17:04 |
| inheritance problem | ap6118 | C++ Forum | 8 | 11-Apr-2005 11:24 |
| Inheritance problem | CaptnB | C++ Forum | 2 | 27-Jan-2005 08:09 |
| Floating point exception error (Note-long post) | crystalattice | C++ Forum | 16 | 11-Sep-2004 07:37 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The