I have read several questions about this problem at SO but the suggested solution is to do the UPDATE 4. I have already installed update-4 to my VS 2012 Ultimate. But still I am not able to get the UML diagram.
My Step:
Go to the architecture tab in VS 2012 Ultimate
select "New Diagram"
select "UML Class Diagram"
then choose the name etc.
the go to "Architecture Explorer"
Drop the classes for which you want to generate UML diagram.
Just to check if there is some other probelm, I wrote a simple program of two classes. In this program, the variable of one class is set by the variable of second class. But there is no UML diagram get generated for the following program.
ERRORS:
Reverse engineer C++ type is not supported. Some DGML node(s) will be skipped during the process. Please see warnings for details.
(0) type(s) fully reverse engineered.
Warnings:
<Warning Timestamp="2015-09-01T09:45:05" Importance="Medium" Text="Reverse engineer C++ type 'Class1' is skipped." />
<Warning Timestamp="2015-09-01T09:45:05" Importance="Medium" Text="Reverse engineer C++ type 'Class2' is skipped." />
My code:
Class1
#ifndef HEADER1_H
#define HEADER1_H
class Class1 {
private:
int value;
public:
void setValue(int value);
void showValue();
};
#endif
Class2:
#ifndef HEADER_H
#define HEADER_H
#include "Header1.h"
class Class2 {
public:
int secondClassValue;
void setValue(int val);
Class1 *class1;
};
#endif
main():
#include <iostream>
#include "Header1.h"
#include "Header.h"
using namespace std;
void Class1::setValue(int value) {
this->value = value;
}
void Class1::showValue() {
cout<<"The value of second class variable is: "<<this->value<<endl;
}
void Class2::setValue(int val) {
secondClassValue = val;
}
int main() {
Class1 *class1 = new Class1;
Class2 *class2 = new Class2;
class2->setValue(10);
class1->setValue(class2->secondClassValue);
//See the set value
class1->showValue();
delete class1;
delete class2;
return 0;
}
Related
I´m building a program with several classes (data structures like stacks, lists,etc).
There is some class (Concesionario) that i need to use in another (ListaE). The class ListaE uses another class called NodoListaE, which uses two pointers, one to the value of the object (Concesionario) and another to the next position of the list (siguiente).
#ifndef NODOLISTAE_HPP
#define NODOLISTAE_HPP
#include "Concesionario.hpp"
class Concesionario;
class ListaE;
class NodoListaE
{
public:
NodoListaE(Concesionario* conc, NodoListaE* sig = NULL);
private:
Concesionario* conc;
NodoListaE* siguiente;
friend class ListaE;
};
typedef NodoListaE* pnodoListaE;
#endif // NODOLISTAE_HPP
#ifndef LISTAE_HPP
#define LISTAE_HPP
#include "NodoListaE.hpp"
#include "Automovil.hpp"
class Automovil;
class NodoListaE;
class ListaE
{
private:
NodoListaE* primero;
public:
ListaE();
void enlistarOrden(Automovil* automovil);
};
#endif // LISTAE_HPP
#ifndef CONCESIONARIO_HPP
#define CONCESIONARIO_HPP
#include <string>
#include "ListaE.hpp"
class ListaE;
class Concesionario
{
public:
Concesionario();
~Concesionario();
std::string mostrar();
void setZona(std::string letra);
void setNum();
int getNum();
private:
int nc=2;
int num_conc;
std::string zona;
int generadorNumsIntervalo(int min, int max);
ListaE automoviles;//ERROR HERE
};
#endif // CONCESIONARIO_HPP
All the cpp files are not implemented (empty constructor and destructor).
The compiler I´m currently using is MINGWx64.
I´ve tried using forward declarations and it worked for the rest of the classes but not for this one.
The program throws the following error in the **Concesionario ** hpp file: include\Concesionario.hpp|22|error: field 'automoviles' has incomplete type 'ListaE'|
Concesionario is implemented in other classes and the program runs perfectly.
Example of another class implementing Concesionario
#ifndef ARBOL_HPP
#define ARBOL_HPP
#include <iostream>
#include "NodoArbol.hpp"
#include "Concesionario.hpp"
using namespace std;
class Arbol {
public:
Arbol();
void Insertar(Concesionario* concesionario);
private:
pnodoArbol raiz;
pnodoArbol actual;
int contador;
int altura;
bool Vacio(pnodoArbol nodo);
};
#endif // ARBOL_HPP
I`ve also tried deleting this class and creating another one from 0 but the error remains.
Any solution to this problem? Thank you very much.
I am trying to use template classes to use my functions regardless of the point type. I read the “Writing a new PCL class” tutorial but I have not got it. I will share the simplest class where I am trying to use this technique. Its only function is to create the KDtree of a pointcloud in the correct point of the execution of a parent tree of processes.
KdtreeBuilder_Process.h
#ifndef KDTREEBUILDER_PROCESS_H
#define KDTREEBUILDER_PROCESS_H
#include "ProcessManager/ProcessConcurrent.h" //Parent class
#include <pcl/kdtree/kdtree_flann.h>
class KdtreeBuilder_Process:public ProcessConcurrent
{
public:
KdtreeBuilder_Process(pcl::PointCloud<pcl::PointXYZ>::Ptr inputCloud,pcl::KdTree<pcl::PointXYZ>::Ptr cloudKdtree);
virtual void run(); //method that executed when the process starts
private:
pcl::PointCloud<pcl::PointXYZ>::Ptr mInputCloud;
pcl::KdTree<pcl::PointXYZ>::Ptr mCloudKdtree;
};
#endif // KDTREEBUILDER_PROCESS_H
KdtreeBuilder_Process.cpp
#include "KdtreeBuilder_Process.h"
KdtreeBuilder_Process::KdtreeBuilder_Process(pcl::PointCloud<pcl::PointXYZ>::Ptr inputCloud,pcl::KdTree<pcl::PointXYZ>::Ptr cloudKdtree):
mInputCloud(inputCloud),mCloudKdtree(cloudKdtree)
{
}
void KdtreeBuilder_Process::run(){
mCloudKdtree->setInputCloud(mInputCloud);
}
My intention is to be able to use this class with any point type that contains XYZ coordinates
Thanks for your support.
BR
First of all when dealing with templates you have to accept that all implementation will need to be moved to header files. If you want KdtreeBuilder_Process to be a template that takes class of point in parameter you just need to add appropriate template declaration syntax:
template<class PointType>
class KdtreeBuilder_Process:public ProcessConcurrent
{
public:
If PointCloud class is ready to accept all classes with XYZ coordinates you just need to change your code accordingly
KdtreeBuilder_Process(pcl::PointCloud<PointType>::Ptr inputCloud,pcl::KdTree<PointType>::Ptr cloudKdtree): mInputCloud(inputCloud), mCloudKdtree(cloudKdtree) { }
virtual void run(){
mCloudKdtree->setInputCloud(mInputCloud);
}
private:
pcl::PointCloud<PointType>::Ptr mInputCloud;
pcl::KdTree<PointType>::Ptr mCloudKdtree;
};
Good luck!
I solve the issue. Here is the final solution using only header file:
KdtreeBuilder_Process.h
#ifndef KDTREEBUILDER_PROCESS_H
#define KDTREEBUILDER_PROCESS_H
#include "ProcessManager/ProcessConcurrent.h"
#include "PointDefinitions.h"
#include <pcl/kdtree/kdtree_flann.h>
#include <QDebug>
template<class PointType>
class KdtreeBuilder_Process:public ProcessConcurrent
{
typedef typename pcl::PointCloud<PointType>::Ptr PointCloudPtr;
typedef typename pcl::KdTree<PointType>::Ptr KdTreePtr;
public:
KdtreeBuilder_Process(PointCloudPtr inputCloud,KdTreePtr cloudKdtree): mInputCloud(inputCloud), mCloudKdtree(cloudKdtree) { }
virtual void run(){
mCloudKdtree->setInputCloud(mInputCloud);
}
private:
PointCloudPtr mInputCloud;
KdTreePtr mCloudKdtree;
};
#endif // KDTREEBUILDER_PROCESS_H
I a beginner in programming.
I coded two classes(having constructors with requirement to pass arguments) and want to declare and use one class's object in another class.
I have tried to find the solution to my error on many website, but none of them worked. I also saw a solution to this problem using the 'new' syntax.
Please suggest some(any) way to sought out this problem.
A short program similar the one in which I am facing problems is as follows:
(I know this program is stupid but, this is not actual program I am facing problem in. Instead this is a narrowed down version of the part of the program in which I am facing error)
The error is in Class2.h and main.cpp
main.cpp
#include <iostream>
#include "Class2.h"
using namespace std;
int main()
{
Class2 Class2_Obj;
Class2_Obj.Class2_Function(); // error: undefined reference to `Class2::Class2_Function
return 0;
}
Class1.h
#ifndef CLASS1_H_INCLUDED
#define CLASS1_H_INCLUDED
class Class1
{
private:
const int c1_Variable;
public:
Class1(int);
// Displays the value of c1_Variable on output screan
void Class1_Function();
};
#endif // CLASS1_H_INCLUDED
Class1.cpp
#include <iostream>
#include "Class1.h"
Class1::Class1(int receivedInt) : c1_Variable(receivedInt) {}
void Class1::Class1_Function()
{
cout << c1_Variable;
}
Class2.h
#ifndef CLASS2_H_INCLUDED
#define CLASS2_H_INCLUDED
#include"Class1.h"
class Class2
{
private:
Class1 Class1_Obj(4); // 4 is just a random number.
//error: expected identifier before numeric constant
//error: expected ',' or '...' before numeric constant
public:
// Calls Class1_Function()
void Class2_Function();
};
#endif // CLASS2_H_INCLUDED
Class2.cpp
#include <iostream>
#include "Class1.h"
#include "Class2.h"
void Class::Class2_Function()
{
Class1_Obj.Class1_Function();
}
Here are the links to snapshots of the errors:
Screenshot of Error in Class2.h - http://i.stack.imgur.com/WpK9k.jpg
Screenshot of Error in main.cpp - http://i.stack.imgur.com/yDBD7.jpg
Please help me out! Thanks in advance for any responses :)
The issue is that this in-place initialization of non-static data members syntax is invalid:
class Class2
{
private:
Class1 Class1_Obj(4);
....
};
You can use {} instead,
class Class2
{
private:
Class1 Class1_Obj{4};
....
};
or this form
class Class2
{
private:
Class1 Class1_Obj = Class1(4);
....
};
C++ is a Object Oriented Language. It has classes to structure its data.
To put one class into another, you make an object of one class a member of another class.
Syntactically, it works like
class A {
int x;
public:
A (int x1) : x(x1) {}
};
class B {
A a; // this is how you do it ..
public:
B() : A(4) {}
};
B b; // b is an object which has a member b.a
As you can see, b is an object of class B. It has a member a of class A.
I am writing a simple banking program with derived classes and I am running into a Multiple definition of <method name> error when including parent class.
Keep in mind that I just started coding in C++ yesterday, and moving over from Java/PHP, handling headers/definitions is a bit confusing for me. Please correct anything you see wrong!
Here is a sample of my files/code:
Files
Account.h
Account.cpp (Super)
ChequingAccount.cpp (Child)
SavingsAccount.cpp (Child)
The error is reproduce-able when including the parent class (Account.cpp) into any file. I have reduced my code by a lot, but it should give you an idea of how I am handling inheritance.
To clarify, when I #include the child classes to any file (ChequingAccount.cpp) works fine, and inherited functions work as expected. However, when I #include the parent class (Account.cpp) breaks the compiler with the Multiple definition of <method name> error for all methods.
Again, I am not sure if this is the proper way to do it, but this is what I understand from tutorials I have found.
Code
Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
protected:
double m_balance;
public:
Account(double balance); // Constructor.
virtual ~Account(); // Destructor.
// Accessor Methods.
double getBalance() const;
// Mutator Methods.
virtual void withdrawFunds(double amount);
void depositFunds(double amount);
};
#endif
Account.cpp (Superclass)
#include "Account.h"
Account::Account(double balance = 0)
{
m_balance = balance;
}
Account::~Account()
{
// TODO: Delete this data structure...
}
double Account::getBalance() const
{
return m_balance;
}
void Account::withdrawFunds(double amount)
{
m_balance -= amount;
}
void Account::depositFunds(double amount)
{
m_balance += amount;
}
ChequingAccount.cpp (Child)
#include "Account.h"
class ChequingAccount: public Account
{
public:
ChequingAccount(int id, int userId, double balance) : Account(id, balance){};
void withdrawFunds(double amount)
{
// Override parent method.
}
};
Any help would be greatly appreciated! Thank you!
When you #include "some file.cpp", you are directing the compiler to copy the contents of that cpp file to that point in the program. This will create two compiled versions of your "some file" which will lead to "Multiple Definitions."
First of all, "identical" functions declared in base class and sub-class will not cause multiple definition error.
Here is just one example and explanation aiming to help you understand my point:
main.cpp
class father{
void fun();
}
void father::fun(){}
class son : public father{
void fun();
}
void son::fun(){}
int main()
{
return 0;
}
You compile it, and definitely no multiple definition error. In Java, we define functions within classes. In C++, only inline functions are defined within classes and others are declarations, which can be declared whatever times you like. See the definition syntax : father::fun son::fun. These actually define two different functions, one is in father and the other is in son. So there are no multiple definition error. Once more, in class declarations you can only define inline functions and can only declare non-inline functions. So there are on multiple definition errors at all. This just aims to help you understand it in a grammar way.
BTW, if compiler failed to inline, there would be no multiple definition error too because even though you define inline functions in header files and include the files anywhere, inline functions are of internal linkage.
I compiled your codes and got different errors regarding your defined constructors. Anyway, including cpp files using "include" means you don't get the point of implementation file and interface file.
You need to declare the child's method as a member of the child class explicitly in the child's .cpp file as well. See below:
ChequingAccount.cpp (Child)
#include "Account.h"
class ChequingAccount: public Account
{
public:
ChequingAccount(int id, int userId, double balance) : Account(id, balance){};
void ChequingAccount::withdrawFunds(double amount)
{
// Override parent method.
}
};
You already have void Account::withdrawFunds(double) defined when you define void ChequingAccount::withdrawFunds(double).
Try virtual void ChequingAccount::withdrawFunds(double)
The virtual keyword is similar to overriding in Java.
In short, you should not include a cpp file.
The reason you get a multiple definition error actually has nothing to do with class inheritance.
Consider a simple example:
add.h:
#ifndef __ADD_H_
#define __ADD_H_
int add(int a);
#endif
add.cpp:
#include "add.h"
int add(int a) { return a + 1; }
main.cpp:
#include "add.h"
int main() { return add(-1); }
The way C++ dealing with #include directive is simply to copy-paste the included file and replace the #include line. So if we expand the above files manually, we will get something like:
add.cpp:
#ifndef __ADD_H_
#define __ADD_H_
int add(int a);
#endif
int add(int a) { return a + 1; }
main.cpp:
#ifndef __ADD_H_
#define __ADD_H_
int add(int a);
#endif
int main() { return add(-1); }
which will work just fine. (We are allowed to have multiple declarations of a function)
However, if you decide to include the .cpp file instead of the .h file like the code below:
main.cpp:
#include "add.cpp" // notice here
int main() { return add(-1); }
And if you expand it as we just did:
add.cpp:
#ifndef __ADD_H_
#define __ADD_H_
int add(int a);
#endif
int add(int a) { return a + 1; }
main.cpp:
#ifndef __ADD_H_
#define __ADD_H_
int add(int a);
#endif
int add(int a) { return a + 1; }
int main() { return add(-1); }
You will see the code includes multiple definitions of the function add, one of which is in add.cpp and the other one is in main.cpp. When linking these two files together (each .cpp file is compiled separately using a compiler and then linked together using a linker), the linker will be confused by two definitions of add and doesn't know which one to use, so it will start to complain.
I have a quite large c++/mpi project in which I want to integrate some cuda-functionality.
I created a sample project (not runnable yet) to illustrate the problem. The comments in between the sources describe the problem.
I have a main.cc:
/*main.cc*/
#include <iostream>
#include "derivedclass.h"
#include "someotherclass.h"
using namespace std;
int main(){
int intstate = 4;
DerivedClass<int> myDerivedClass;
myDerivedClass.setState(intstate);
myDerivedClass.compute();
int result = myDerived.getResult();
SomeOtherClass mySomeOtherClass(result);
mySomeOtherClass.print();
}
which includes some c++ class(es):
/*someotherclass.h*/
#ifndef INTEGRATOR_H_GUARD
#define INTEGRATOR_H_GUARD
class SomeOtherClass{
private:
int someVariable;
public:
SomeotherClass(int someVariable);
void print();
};
#endif
/*someotherclass.cc*/
#include "someotherclass.h"
SomeOtherClass::SomeOtherClass(int someVariable){
this->someVariable = someVariable;
}
SomeOtherClass::print(){
cout << this->someVariable << endl;
}
These c++ parts a quite large and I don't won't to change them.
There is some baseclass:
/*baseclass.h*/
#ifndef BASECLASS_H_GUARD
#define BASECLASS_H_GUARD
class BaseClass{
protected:
int someVariable;
public:
BaseClass(int someVariable);
void compute();
int getResult();
};
#endif
/*baseclass.cc*/
BaseClass::BaseClass(int someVariable){
this->someVariable = someVariable;
}
void BaseClass::compute(){
/* do something*/
}
int BaseClass::getResult(){
return this->someVariable;
}
This base class provides a lot of functionality (too much but not my project so I can't change that).
Some methods are to be parallelized by me. So for me it seems to be the cleanest way to derive the BaseClass with a Class using Cuda:
/*derivedclass.h*/
#ifndef DERIVEDCLASS_H_GUARD
#define DERIVEDCLASS_H_GUARD
#include "baseclass.h"
class DerivedClass: public BaseClass{
public:
DerivedClass(int someVariable);
void compute();
};
#endif
/*derivedclass.cu*/
#include "derivedclass.h"
DerivedClass::DerivedClass(int someVariable):
BaseClass(someVariable);
{
}
void DerivedClass::compute(){
/* do some cuda stuff*/
}
So I have the following questions:
Is it possible to derive a cuda-class from a c++-class
When I try to compile the main.cc class with g++ and the .cu classes with nvcc I get an error when using cuda stuff in the .cu class for example cudamalloc:
./Folder/class.cu:line: Fehler: expected initializer before »cudaMalloc«
When I try to compile the main class with nvcc I get problems with MPI-functionality.
So is it possible to use a cuda-class from a main.cc compiled with g++/mpicc (which seems to be the right way round for me)?
I hope I described my problem understandable and appreciate any help.
From what I can tell from your code, your derived class is just a C++ class that launches CUDA kernels. So it's not really a "CUDA class". So the answer to your question 1. is "yes", you can do that. You just have to put any member functions that launch kernels in .cu files with the kernels they launch.
The error "expected initializer before 'cudaMalloc'" is a weird syntax error. It doesn't really seem to have anything to do with your derived class stuff. I suspect you just have a C++ syntax error.