Any and all help is greatly appreciate. Thank you for taking the time out to review my issue.
I am currently receiving an errors
1>c:\users\fordn_000\documents\tcc_odu\it310\programs\it310_homework_program_2_nford\it310_homework_program_2_nford\Form1.h(625): error C2653: 'Marshal' : is not a class or namespace name
1>c:\users\fordn_000\documents\tcc_odu\it 310\programs\it310_homework_program_2_nford\it310_homework_program_2_nford\Form1.h(625): error C3861: 'StringToHGlobalAnsi': identifier not found
This is my GUI form code, I want to use the command marshal however, that appears where there error is taking place
private: System::Void DisplayButton_Click(System::Object^ sender, System::EventArgs^ e)
{
int InitProductID = 0;
char* InitDescription;
int InitManufID = 0;
double InitWholeSale = 0.0;
double InitMarkup = 0.0;
int InitQuanity = 0;
String^ TypeString;
//EXTRACT FROM INPUT TEXT BOX'S
InitProductID = Convert::ToInt32(ProductIDNumberBoxNew->Text);
InitDescription = (char*)(void*)Marshal::StringToHGlobalAnsi(DescriptionBox->Text);
InitManufID = Convert::ToInt32(ManufacturerBox->Text);
InitWholeSale = Convert::ToDouble(WholesalePriceBox->Text);
InitMarkup = Convert::ToDouble(MarkupBox->Text);
InitQuanity = Convert::ToInt32(QuantityBox->Text);
//CREATE INSTANCE OF CLASS
Inventory InventoryItem(InitProductID, InitDescription, InitManufID, InitWholeSale, InitMarkup, InitQuanity);
//DISPLAY TO OUTPUT TEXT BOXS
ProductIDNumberOutBox->Text = Convert::ToString(InventoryItem.GetProductID());
TypeString=gcnew String(InventoryItem.GetDescription());
ManufacturerOutBox->Text = Convert::ToString(InventoryItem.GetManufID());
//RETAIL PRICE OUTBOX
QuantityOutBox->Text= Convert::ToString(InventoryItem.GetQuanity());
}
This is my stdafx header file below
#pragma once
// TODO: reference additional headers your program requires here
#include "Inventory.h"
This is my stdafx cpp file below
#include "stdafx.h"
#include "Form1.h"
Finally this is my inventory header file
//SPECIFICATION FILE (INVENTORY.H)
#ifndef INVENTORY_H
#define INVENTORY_H
#include <iostream>
#include <iomanip>
using namespace std;
class Inventory
{
private:
int ProductID;
mutable char Description[25];
int ManufID;
double WholeSale;
double Markup;
int Quanity;
public:
//CONSTRUCTORS
Inventory( );
Inventory(int, char[], int, double, double, int);
//GET FUNCTIONS
int GetProductID( )const;
char* GetDescription( )const;
int GetManufID( )const;
double GetWholeSale( )const;
double GetMarkup( )const;
int GetQuanity( )const;
//DISPLAY FUNCTION
void Display( )const;
//RETURN FUNCTION
double RetailPrice( )const;
};
#endif
I think you need to reference this:
using namespace System::Runtime::InteropServices;
Related
I just started working with C++ and am working on an exercise that deals with polymorphic pointers. I'm having trouble trying to solve an error message I believe I'm getting from my Rectangle.cpp as I call the class from my main.cpp.
The error message:
undefined reference to 'Rectangle::Rectangle(double, double)'
Main.cpp
#include <iostream>
#include "Rectangle.h"
using namespace std;
//////////////////////////////////////////////
// --- FUNCTIONS DECLARATION---
void introduceShape(Shape*);
double calculateShapeArea(Shape*);
double calculateShapeCircumferece(Shape*);
int main()
{
Rectangle rectangle1(5,2);
// Rectangle *rec1 = new Rectangle(5,2);
introduceShape(&rectangle1);
cout << "My area is: " << calculateShapeArea(&rectangle1) << ", my circumference is: " << calculateShapeCircumferece(&rectangle1) << endl << endl;
return 0;
}
//////////////////////////////////////////////
// --- FUNCTIONS ---
void introduceShape(Shape* shapeToIntroduce)
{
return shapeToIntroduce->introduce();
}
double calculateShapeArea(Shape* shapeToCalculate)
{
return shapeToCalculate->calculateArea();
}
double calculateShapeCircumferece(Shape* shapeToCalculate)
{
return shapeToCalculate->calculateCircumference();
}
Rectangle.h
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"
#include <iostream>
using namespace std;
class Rectangle: public Shape
{
double width;
double height;
public:
Rectangle(double , double );
void introduce();
double calculateArea();
double calculateCircumference();
};
#endif // RECTANGLE_H_INCLUDED
Rectangle.cpp
#include "Rectangle.h"
#include <iostream>
using namespace std;
Rectangle::Rectangle(double width, double height)
{
this->width = width;
this->height = height;
}
void Rectangle::introduce()
{
cout << "I AM A RECTANGLE !" << endl;
}
double Rectangle::calculateArea()
{
return width*height;
}
double Rectangle::calculateCircumference()
{
return 2*(width+height);
}
Shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
class Shape
{
public:
virtual void introduce() = 0;
virtual double calculateArea() = 0;
virtual double calculateCircumference() = 0;
};
#endif // SHAPE_H_INCLUDED
The error is generated by the linker because it can not see where the definition of the constructor is located.
If you are using an IDE, you should add .cpp file to the project so that it can be compiled and the definition would be found by the linker. It not, then you have to compile it yourself -assuming you are using gcc:
g++ Rectangle.cpp
will combine cpp files into one executable and should not show you that error.
Visit this post
I am a novice at C++. I have started writing a rather large code on a project and I want to make a good base for that. I get the following error when running in Visual Studio:
Exception thrown: read access violation. this was nullptr. occurred
After searching similar questions, I could not find the solution for my code. The error is occurring in Driver.cpp at:
m_setup->SetEquation(...)
Any help is really appreciated. I also appreciate if you have suggestion to improve my code structure.
Main.h:
#ifndef FEM_AD1D_MAIN_H
#define FEM_AD1D_MAIN_H
namespace FEM_AD1D
{
class Driver;
Driver* m_driver;
}
#endif
Main.cpp:
#include "Main.h"
#include "Driver.h"
#include <iostream>
using namespace FEM_AD1D;
int main()
{
std::cout << "Hello World!\n";
m_driver->Run();
}
Driver.h:
#ifndef FEM_AD1D_DRIVER_H
#define FEM_AD1D_DRIVER_H
namespace FEM_AD1D
{
class Setup;
class Driver
{
public:
Driver(Setup* setup);
void Run();
void PreProc();
private:
Setup* m_setup;
const int m_nDim = 1;
const double m_aCoef = 1.0;
const double m_bCoef = 3.0;
const double m_cCoef = 0.0;
const double m_fCoef = 1.0;
const double m_xMin = 0.0;
const double m_xMax = 1.0;
const int m_nElem = 5;
const int m_elemType = 2;
const double m_meshStretch = 0.0;
const int m_nGaussPoint = 3;
};
}
#endif
Driver.cpp:
#include "Driver.h"
#include "Setup.h"
#include <iostream>
#include <array>
using namespace FEM_AD1D;
Driver::Driver(Setup* setup)
{
m_setup = setup;
//m_setup = new Setup();
}
void Driver::Run()
{
PreProc();
}
void Driver::PreProc()
{
std::cout << "Hello World 2!\n";
m_setup->SetEquation(m_nDim, m_aCoef, m_bCoef, m_cCoef, m_fCoef);
}
Setup.h:
#ifndef FEM_AD1D_SETUP_H
#define FEM_AD1D_SETUP_H
#include <vector>
namespace FEM_AD1D
{
class Setup
{
public:
Setup();
void SetEquation(int nDim, double aCoef, double bCoef, double cCoef, double fCoef);
private:
int m_nDim;
double m_aCoef;
double m_bCoef;
double m_cCoef;
double m_fCoef;
};
}
#endif
Setup.cpp:
#include "Setup.h"
#include "Driver.h"
#include <iostream>
#include <vector>
using namespace FEM_AD1D;
Setup::Setup() :
m_nDim(0),
m_aCoef(0.0),
m_bCoef(0.0),
m_cCoef(0.0),
m_fCoef(0.0)
{
}
void Setup::SetEquation(int nDim, double aCoef, double bCoef, double cCoef, double fCoef)
{
// Set the coefficients of the equation
std::cout << "Hello World 3!\n";
m_nDim = nDim;
m_aCoef = aCoef;
m_bCoef = bCoef;
m_cCoef = cCoef;
m_fCoef = fCoef;
}
Im trying to create the core in main , then create x Players. I would like to follow the singleton structure for the other classes that i will code but I would like to get to the root of the cause of this error I'm receiving from the compiler. I have tried different approaches to trying to solve but I'm new to c++ so this making things extremely difficult.
These are the errors I'm receiving
errors
./Core.h:18:39: error: expected '(' for function-style cast or type construction
CreateNewPlayer(std::string ScreenName, int ID) {
~~~~~~~~~~~ ^
./Core.h:18:55: error: expected '(' for function-style cast or type construction
CreateNewPlayer(std::string ScreenName, int ID) {
~~~ ^
app.cpp:10:11: error: no member named 'CreateNewPlayer' in 'Core'
CoreObj.CreateNewPlayer("DONKEYSHARK", 1);
app.cpp
#include "Core.h"
#include "player.h"
int main()
{
Core CoreObj;
CoreObj.CreateNewPlayer("screenname", 1) PlayerOne;
std::cout << CoreObj.GetPlayerScreenName(PlayerOne) << "\n";
std::cout << CoreObj.GetPlayerID(PlayerOne) << "\n";
return 0;
}
Core.h
include<iostream>
#include"Player.h"
#include <vector>
#ifndef Core_H
#define Core_H
class Core{
private:
public:
/* The CORE is Managing all the objects so it needs to know what job in what sector its going to perform in
the first parameter will be what sector, second task, third data*/
Core(int x = 0, std::string y = "New"){
/* Need to know which sector the task belongs to, Becuase this class is going to get complex Qucikly */
switch(x){
case 0:
CreateNewPlayer(std::string ScreenName, int ID) {
Player(ScreenName, ID) Player;
return Player&;
}
break;
default:
/* log function*/
break;
}
}
int GetPlayerID(Player& Player){
return Player.GetID();
}
std::string GetPlayerScreenName(Player& Player){
return Player.GetScreenName();
}
};
#endif /*Core_H*/
Player.h
include <iostream>
#ifndef Player_H
#define Player_H
class Player {
private:
std::string ScreenName, SignUpDate, PublicKey, PrivateKey;
int PlayerID, CreditBalance, GlobalRank, RegionalRank, localRank;
friend class Core Player(std::string, int);
public:
int TotalPlayers = 0, TotalLivePlayers = 0;
static std::string GetScreenName();
static int GetID();
Player(std::string Screenname, int ID){
this->ScreenName = Screenname;
this->PlayerID = ID;
TotalPlayers++;
TotalLivePlayers++;
}
}
player.cpp
#include <iterator>
#include <iostream>
#include <ctime>
#include "PlayerKeys.h"
class Player {
private:
std::string ScreenName, SignUpDate, PublicKey, PrivateKey;
int PlayerID, CreditBalance, GlobalRank, RegionalRank, localRank;
static int TotalPlayers, TotalLivePlayers;
public:
static int GetTotalNumbOfPlayer(){return TotalPlayers;}
static int GetTotalNumbOfLivePlayer(){return TotalLivePlayers;}
int GetID(){return PlayerID;}
std::string GetScreenName(){return ScreenName;}
Player(){
/* overload*/
}
static int GetTotalNumbOfLivePlayer(){return TotalLivePlayers;}
static int GetTotalNumbOfPlayer(){return TotalPlayers;}
~Player();
}
core.cpp
#include<iostream>
#include"Player.h"
/* core auto Deck = GetDeck();
// std::copy(Deck.begin(), Deck.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << "\n";*/
class Core{
/* As a security measure when core functionality needs the daily hash values */
public:
std::string Core::DevlopmentTeamAccessDailyHash(){
return "DailyHAshValueTest"
}
std::sting Core::SystemAccessActiveHash(){
return "SystemHAshValueTest"
}
std::string Core::CurrentDateAndTime(){
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
std::string str(buffer);
return str;
}
}
I have a problem. In my Qt project I'm having an error. Tt says:
multiple definition of estructura
I think it is because I'm using too many "#includes", but I don't know how to fix it.
This is my program:
Struct.h
#ifndef STRUCT_H
#define STRUCT_H
#include <stdio.h>
#include <string>
struct Circulo{
std::string nombre;
int capacidad;
int statusMujer[4];
int hijos[4];
};
struct Circulo estructura[30];
#endif // STRUCT_H
Logica.h
#ifndef LOGICA_H
#define LOGICA_H
#include <iostream>
void madresStatusCodificados(std::string , int , int[]);
void ListadoDeCirculos(int, int , std::string []);
std::string CirculoMayorCapacidad(int );
int NinnosEnCirculosconStatus(int , int );
void Listado(int);
#endif // LOGICA_H
interfaz.cpp
#include <Interfaz/Interfaz.h>
#include <Logica/Defecto.h>
#include <Interfaz/Menu.h>
#include <Logica/Struct.h>
#include <iostream>
using namespace std;
void Principal(){
cin>>estructura[i].statusMujer[3];
cout<<"Escriba la cantidad de hijos con madres desconocidas"<<endl;
cin>>estructura[i].hijos[3];
i++;
}else{
break;
}
}
}
Logica.cpp
#include <iostream>
using namespace std;
//#include <Logica/Logica.h>
#include <Logica/Struct.h>
void madresStatusCodificados(string t, int h , int k[]){
for(int i=0;i<h;i++){
if(estructura[i].nombre==t){
k[0]=estructura[i].statusMujer[0];
k[1]=estructura[i].statusMujer[1];
k[2]=estructura[i].statusMujer[2];
k[3]=estructura[i].statusMujer[3];
}
}
}
void ListadoDeCirculos(int g,int u,string h[]){
for(int i=0;i<u;i++){
if(estructura[i].statusMujer[g-1]!=0)
h[i]=estructura[i].nombre;
}
}
string CirculoMayorCapacidad(int k){
int n=-1;
string l;
for(int i=0;i<k;i++){
if(estructura[i].capacidad>n){
n=estructura[i].capacidad;
l=estructura[i].nombre;
}
}
return l;
}
int NinnosEnCirculosconStatus(int a,int b){
int h=0;
for(int i=0;i<b;i++){
h=h+estructura[i].hijos[a-1];
}
return h;
}
void Listado(int y){
for(int i=0;i<y;i++){
cout<<"NOMBRE DEL CIRCULO CAPACIDAD Madres solteras Madres trabajadoras Madres caso social Madres desconocidas "<<endl;
cout<<estructura[i].nombre<<""<<estructura[i].capacidad<<estructura[i].statusMujer[0]<<estructura[i].statusMujer[1]<<estructura[i].statusMujer[2]<<estructura[i].statusMujer[3]<<endl;
}
}
I am getting this error:
Errors
You have defined a variable named "estructura" in several files, the error tells you that it was first defined in Struct.h being called #included in Logica.cpp,
struct Circulo estructura[30];
you have another definition the the file Defecto.h or a file included by Defecto.h
Move the definition of estructura in a cpp file of your choice, then declare it as extern wherever you need it. And you can omit the struct keyword, in c++.
In a struct.cpp file:
#include "Struct.h"
Circulo estructura[30];
In your Logica.cpp and Interfaz.cpp files:
#include "Struct.h"
extern Circulo estructura[30];
Here is a long and detailed explanation of your issue.
recieving this error
i know its something to do with declaration and definition mis-match but i cant seem to put my finger on it.
Any help would be appreciated :)
IntelliSense: declaration is incompatible with "<error-type>
Airport::getDetails(std::string)" (declared at line 16 of "z:\documents\visual studio 2010\projects\oo_cw\oo_cw\Airport.h")
z:\documents\visual studio 2010\projects\oo_cw\oo_cw\airport.cpp 50 22 OO_CW
Heres my header file and cpp file
header
#pragma once
#include <string>
#include "std_lib_facilities.h"
class Airport
{
public:
Airport();
~Airport(void);
//Modifiers
void setName(string);
void addConnection(string,double,double);
void setTax(double);
//Accessors
string getName();
connections getDetails(string) const ;
double getTax();
private:
string name;
Vector<connections> destinations;
double tax;
};
cpp
#include "Airport.h"
#include <string>
#include "std_lib_facilities.h"
using namespace std;
struct connections {
string destName;
double price;
double time;
};
Airport::Airport()
{
name = "";
tax = 0.0;
};
Airport::~Airport(void)
{
};
void Airport::setName(string airportName){
Airport::name = airportName;
}
void Airport::setTax(double airportTax){
tax = airportTax;
}
void Airport::addConnection(string conName, double conPrice, double conTime){
connections newConnection;
newConnection.destName = conName;
newConnection.price = conPrice;
newConnection.time = conTime;
destinations.push_back(newConnection);
}
string Airport::getName(){
return name;
}
double Airport::getTax(){
return tax;
}
connections Airport::getDetails(string reqName) const {
for(int i =0;i<destinations.size();i++){
if(destinations[i].destName==reqName){
return destinations[i];
}
}
}
You have to put connections definition above class Airport definition in the header file.
Your function Airport::getDetails() must return something for the trivial case, where destinations.size() == 0, (see code below).
Is there a particular reason not to use std::vector in the place of Vector?
connections Airport::getDetails(string reqName) const {
for (int i = 0; i<destinations.size(); i++){
if (destinations[i].destName == reqName){
return destinations[i];
}
}
return connections{"", 0.0, 0.0};
}
You aren't declaring connections before you use it in "Airport.h".