Another C++ Problem

TheJokerV

Weaksauce
Joined
Mar 23, 2007
Messages
81
Ok so I've been trying to debug this thing for about 2 hours now and I'm stuck. Basically I have to turn this .h file which contains a class

original.h
Code:
#ifndef MYVECTOR_H
#define MYVECTOR_H

class MyVector {

private:
  double *vector;
  int length;

 public:
  //No arg default constritor
  MyVector() {}
 
 // Constructor (initFunction)
 MyVector(int size) : vector( new double [size] ), length(size) {}	       
  
  // Destructor (clean up function )
   virtual ~MyVector(){ delete [] vector; length=0; }

// Version used for debugging destructor!
// virtual ~MyVector(){ printf("destructor %d\n", length); if (length) delete [] vector; length=0; }

  // Want to know length of vector for loops, but can't touch it
  // because it is now private. Here I return a copy.
  int getLength(void) const { return length; }

  // Array indexing – so I can treat vector like an array 
  // This allows me to change the value in the vector (LHS of =)
  double& operator[]( int i ) { return vector[i]; }
 
  // Array indexing – this is read only access (RHS of =)
  const double& operator[]( int i )  const { return vector[i]; }

};
               
#endif MYVECTOR_H

into a .cpp/.h class files. I got so far:

new.h
Code:
#ifndef MYVECTOR_H
#define MYVECTOR_H

class MyVector
{
 private:
  double *vector;
  int length;
  
 public:
  MyVector();
  MyVector(int size);
  virtual ~MyVector();
  int getLength(void);
  double& operator[](int i);
  const double& operator[](int i);
}
#endif MYVECTOR_H

new.cpp
Code:
#include "myvector.h"

MyVector::MyVector(){}
MyVector::MyVector(int size):vector(new double [size]), length(size){}
MyVector::virtual ~MyVector(){delete [] vector; length = 0; }
int MyVector:: getLength(void) const {return length;}
double& MyVector::operator[](int i){return vector[i];}
const double& MyVector:: operator[](int i) const {return vector[i]}

This gives me MANY error messages mostly about the operator overloading functions. I tryed a couple of different variation but i narrowed it down to this. Basically this is a homemade vector class. If anyone knows what's wrong the help would be greatly appreciated.

BTW: Does any1 know how to find th Euclidian distance between two vectors? I know its random but I've been searching google awhile and only can find the Euclidian distance between 2 points.:confused:

Anyway thanks for all your help guys.
 
Mathematically speaking, there is no such thing as euclidean distance between two vectors. A vector represents a direction. How would a distance between two directions work? The best you can hope for is to place both vectors at the origin and find the euclidean distance between their end points.

I'll get to your code in a minute. :)
 
1) You must always end a class definition with a semi-colon.
2) You only need to use the 'virtual' keyword in the header file. It doesn't make any sense in the implementation file.
3) Your 'const's need to match up. (eg. on getLength())
4) You need a semi-colon after 'vector' in your second 'operator[]' function.
5) Lastly spend some time getting familiar with 'const'. This will help with number 3). Your operator[] functions should look like this:
Code:
const double& operator[](int i) const;
double& operator[](int i);
The const at the end is part of the function definition. The one in the beginning is part of the return type.
 
Mathematically speaking, there is no such thing as euclidean distance between two vectors. A vector represents a direction. How would a distance between two directions work? The best you can hope for is to place both vectors at the origin and find the euclidean distance between their end points.
Erm. The distance between vectors u and v is defined as ||u - v||, Euclidean space or otherwise.
 
Thanks Tramontane. I got it working thanks to you. I think I just got a little confused with the const expressions. I tried modifying the class a bit but now I got a new problem.

Modified Code:

myvector.h
Code:
#ifndef MYVECTOR_H
#define MYVECTOR_H

class MyVector
{
 private:
  double *vector;
  int length;
  
 public:
  MyVector();
  MyVector(int size);
  virtual ~MyVector();
  int getLength(void) const;
  double& operator[](int i);
  const double& operator[](int i) const;
  void resize(int n);
};
#endif MYVECTOR_H

myvector.cpp
Code:
#include "myvector.h"

MyVector::MyVector(){}
MyVector::MyVector(int size):vector(new double [size]), length(size){}
MyVector::~MyVector(){delete [] vector; length = 0; }
int MyVector:: getLength(void) const {return length;}
double& MyVector::operator[](int i){
  if (i < 0|| i >= length){
    cerr << "index out of range error" << endl;
    exit(1);
  }
  return vector[i];
}
const double& MyVector:: operator[](int i) const {
  if (i < 0|| i >= length){
    cerr << "index out of range error" << endl;
    exit(1);
  }
  return vector[i];
}
void MyVector::resize(int n)
{
  delete [] vector;
  vector = new double [n];
  if( vector == 0 ){
    cerr << "Failed to allocate vector in resize" << endl;
    exit(1);
  }
  length = n;
}

main.cpp (tester program)
Code:
#include <iostream>
using namespace std;
#include "myvector.h"
#include "euclid.h"

int main()
{ 
  Euclid vecA(3);
  cout << "length of vecA =  " << vecA.getLength() << endl; 

  for(int i =0 ; i < vecA.getLength(); i++ ) vecA[i] = (i+1);

  for(int i= 0; i < vecA.getLength(); i++ ) cout << vecA[i] << endl;

  cout <<"Norm2 of  vecA  = " << vecA.norm2(vecA)<< endl;
  
  vecA.resize(4);

  cout << "After resize: " << endl;

  for(int i =0 ; i < vecA.getLength(); i++ ) vecA[i] = (i+1);

  for(int i= 0; i < vecA.getLength(); i++ ) cout << vecA[i] << endl;

  cout <<"Norm2 of  vecA  = " << vecA.norm2(vecA)<< endl;

  return 0;
}

Now when I compile the code I get the following error messages:
myvector.cpp:9: error: ‘cerr’ was not declared in this scope
myvector.cpp:9: error: ‘endl’ was not declared in this scope
myvector.cpp:10: error: ‘exit’ was not declared in this scope
myvector.cpp: In member function ‘const double& MyVector::eek:perator[](int) const’:
myvector.cpp:16: error: ‘cerr’ was not declared in this scope
myvector.cpp:16: error: ‘endl’ was not declared in this scope
myvector.cpp:17: error: ‘exit’ was not declared in this scope
myvector.cpp: In member function ‘void MyVector::resize(int)’:
myvector.cpp:26: error: ‘cerr’ was not declared in this scope
myvector.cpp:26: error: ‘endl’ was not declared in this scope
myvector.cpp:27: error: ‘exit’ was not declared in this scope
]

So I'm guessing for some reason\ the cerr, endl and exit functions aren't defined. But I thought they were defined because they are either in the basic C++ libraries or the iostream library. I don't understand how they can't be defined. I'm confused:confused:
 
cerr, endl, et al are defined in namespace std.

So, in myvector.cpp, either add "using namespace std" to the top of the file, or prepend every usage of those items with "std::" .
 
#include <stdio.h> perhaps?
Nope didn't work(added it to the top of my main file BTW)
cerr, endl, et. al. are defined in namespace std.

So, in myvector.cpp, either add "using namespace std" to the top of the file, or prepend every usage of those items with "std::"

But why would I need to do that if I already said using namespace std in the main file?
 
cerr, endl, et al are defined in namespace std.

So, in myvector.cpp, either add "using namespace std" to the top of the file, or prepend every usage of those items with "std::" .

Also that didn't work. Any more ideas anyone?
 
[TheJokerV]
> But why would I need to do that if I already said using namespace std in the main file?

"using" and "#include" declarations are not global; they only apply to individual translation units (read: .cpp files and their header dependencies).

> Also that didn't work.

You also need to include iostream and whatever else you're using.
 
[TheJokerV]
> But why would I need to do that if I already said using namespace std in the main file?

"using" and "#include" declarations are not global; they only apply to individual translation units (read: .cpp files or .cpp/.h pairs).

Oh ya, upon looking further into your code, you only put the include for stdio in your main.cpp, yet you call cerr in your vector class still. If you put this include in your .h file, it will be included in both the cpps, as both cpps include your .h file. I'm about to attempt to give your code a run right now, so I can give you better feedback if need be.
 
[TheJokerV]
> But why would I need to do that if I already said using namespace std in the main file?

"using" and "#include" declarations are not global; they only apply to individual translation units (read: .cpp files and their header dependencies).

> Also that didn't work.

You also need to include iostream and whatever else you're using.

You were right man. I got it to work. Thanks for your help.

rgratto2: That's actually a really good idea. I basically just put the iostrwam and namespace lines in the cpp file but including it into the .h file would be better for my purposes. Thanks for your help.
 
rgratto2: That's actually a really good idea. I basically just put the iostrwam and namespace lines in the cpp file but including it into the .h file would be better for my purposes. Thanks for your help.

Sweet, good luck with your endeavors.
 
[TheJokerV]
> I basically just put the iostrwam and namespace lines in the cpp file but
> including it into the .h file would be better for my purposes.

Never, ever put a "using" directive inside a header file at global scope. Ever. It's not a great idea to include stuff in a header that it doesn't need either. See http://www.thescripts.com/forum/thread450924.html.
 
Erm. The distance between vectors u and v is defined as ||u - v||, Euclidean space or otherwise.

Ok, so I spoke a little quickly when saying "mathematically speaking". However, what I posted matches your function. I still think it's strange to refer to distances between things that have no fixed point in space.
 
Getting OT, but might be interesting to some...

Mathematically speaking, a vector is simply a set for which we have defined both addition and multiplication operators (obeying a number of axioms I can't remember offhand). It is actually possible to define totally nonsensical vector spaces which have no relation to real life.

Direction, position, distance, etc. are properties which we have found "real life" correlations for in the common vector spaces, but do not inherently correspond to anything in real life (as with everything in mathematics). I like to think of this as being a very useful coincidence.

Regarding using vectors in Euclidian space to represent position, this is a very common practise in physics, and I'm sure other disciplines.
 
Back
Top