[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Problem passing a vector class pointer as reference

Posted on 2008-06-12
5
Medium Priority
?
1,318 Views
Last Modified: 2013-12-14
Using an example of code below; if I run the "main.h" and "main.cpp" file by themselves all works fine. Unfortunately I need to view the vector's object from another file. I can only guess that I need to pass the vector by reference. After searching and trying many many things I'm still unsure of "the call", "the phototype" and "fuction definition" to make this work. Below are four files of the example. Any suggestions?

//main.h
#ifndef MAIN_H
#define MAIN_H

#include <iostream>
#include <iomanip>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using namespace std;

class Shape {
public:
      void setCenterX(int x);
      int getCenterX() { return x; }

      void setCenterY(int y);
      int getCenterY() { return y; }
      
      virtual void printShapeName() const = 0;
      virtual void printCenter() const;
      virtual void print() const = 0;
      virtual void printArea() const = 0;

private:
      int x;
      int y;
};

void Shape::setCenterX(int cX) { x = cX; }

void Shape::setCenterY(int cY) { y = cY; }

void Shape::printCenter() const { cout << "Location=" << '[' << x << "," << y << ']'; }

class Circle : public Shape {
public:
      Circle( int x = 0, int y = 0, double r = 0.0 );
      double getRadius() const { return radius; }
      virtual void printShapeName() const { cout << "Circle:"; }
      virtual void print() const;
      virtual void printArea() const;
            
private:
      double radius;
};
#endif

//main.cpp
#include "main.h"
//#include "view.cpp"
#include <iostream>
#include <iomanip>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using namespace std;
using std::ios;
using std::setiosflags;
using std::setprecision;

Circle::Circle( int cX, int cY, double cR )
{
      setCenterX(cX);
      setCenterY(cY);
      radius = cR;
}

void Circle::print() const { cout << ", radius=" << radius; }

void Circle::printArea() const { cout << ", Area=" << 3.14159 * radius * radius << endl; }

int main()
{
      cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
      vector<Shape *> sh;      
      sh.push_back(new Circle(1, 1, 2.0));
      
      //this prints fine
      int i = 0;
      sh[i]->printShapeName();
      sh[i]->printCenter();
      sh[i]->print();
      sh[i]->printArea();
      cout << endl << endl;

      return 0;
}

//view.h
#ifndef VIEW_H
#define VIEW_H

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

class viewShape {
public:
      void viewTheShape(vector<Shape *> &);

private:      
};
#endif

//view.cpp
#include "main.h"
#include "view.h"
#include <iostream>
#include <iomanip>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using namespace std;
using std::ios;
using std::setiosflags;
using std::setprecision;

void viewTheShape(vector<Shape *> &sha)
{
      cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
      int i = 0;
      sha[i]->printShapeName();
      sha[i]->printCenter();
      sha[i]->print();
      sha[i]->printArea();
      cout << endl << endl;
}
0
Comment
Question by:Enview
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 21776115
at view.cpp, this line:
void viewTheShape(vector<Shape *> &sha)

should be:
void viewShape::viewTheShape(vector<Shape *> &sha)
0
 

Author Comment

by:Enview
ID: 21776150
jaime olivares,
I made the change, now receiveing:
error LNK2005: "public__void thiscall Shape::setCenterX(int)" (?setCenterX@Shape@@QAEXH@Z already defined in main.obj.     Another same error for setCenterY and PrintCenter

I'm really not sure what to think?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21776211
have you removed the comment slashes before #include "view.cpp" ?
0
 

Author Comment

by:Enview
ID: 21776272
Yes, and changed it to view.h    ...... also remove/changed the three lines in main.h
void Shape::setCenterX(int cX) { x = cX; }   ** moved the last part up: void setCenterX(int cX) { x = cX; }
void Shape::setCenterY(int cY) { y = cY; }  **moved the last part up: void setCenterY(int cY) { y = cY; }
void Shape::printCenter() const { cout << "Location=" << '[' << x << "," << y << ']'; }  **moved the last part up: virtual void printCenter() const { cout << "Location=" << '[' << x << "," << y << ']'; }

all complies fine with no LNK errors.
Also, I added these two lines to the "main.cpp" file
      viewShape V;
      V.viewTheShape(sh);
Everything works great! Thanks for stepping me through the problem!
0
 

Author Closing Comment

by:Enview
ID: 31466826
Thank you for stepping me through this problem.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question