C++ shared pointer issue [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to create a set function that will take in a shared pointer and set it equal to another shared pointer.
This is the shared pointer and the set function that i have declared on my header file
class Shape
{
public:
Shape();
Gdiplus::Point start;
Gdiplus::Point end;
std::shared_ptr<Gdiplus::Pen> m_pen;
virtual LRESULT Draw(Gdiplus::Graphics * m_GraphicsImage) = 0;
void setPen(std::shared_ptr<Gdiplus::Pen> pen2);
void setStart(int xPos, int yPos);
void setEnd(int xCor, int yCor);
};
but when i try to implement it in my cpp i get an error saying that my "declaration is incompatible with void setPen declared on .h". it also says m_pen is unedintified on my cpp file.
#include<memory>
#include "stdafx.h"
#include "resource.h"
#include "ShapeMaker.h"
void Shape::setPen(std::shared_ptr<Gdiplus::Pen> pen2)
{
m_pen = pen2;
}
void Shape::setStart(int xPos, int yPos)
{
start.X = xPos;
start.Y = yPos;
}
void Shape::setEnd(int xCor, int yCor)
{
end.X= xCor;
end.Y = yCor;
}
That's literally all i have. the stdax.h includes
#include <atlwin.h>
#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>
#include <atlctrlw.h>
#include <atlscrl.h>
#include <GdiPlus.h>
Errors that I get:
shapemaker.h(11): error C2039: 'shared_ptr' : is not a member of 'std'
shapemaker.h(11): error C2143: syntax error : missing ';' before '<'
shapemaker.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
shapemaker.h(11): error C2238: unexpected token(s) preceding ';'
.h(16): error C2039: 'shared_ptr' : is not a member of 'std'
shapemaker.h(16): error C2061: syntax error : identifier 'shared_ptr'
shapemaker.cpp(9): error C2511: 'void Shape::setPen(std::tr1::shared_ptr<_Ty>)' : overloaded member function not found in 'Shape'

I'm posting my answer from the comments for any visitors.
The problem is at the beginning of the cpp file.
#include<memory>
#include "stdafx.h"
#include "resource.h"
#include "ShapeMaker.h"
MSVC++ demands that the precompiled header "stdafx.h" precede any other code in your source files.
The #include<memory> must be removed, and instead placed in "ShapeMaker.h" where it is first needed.

Related

Why is my class object causing undeclared identifier errors?

I am writing a program on Microsoft Visual C++ 2010 Express and am having an issue with my class object. I keep getting undeclared identifier and missing ';' before identifier errors and I'm not sure what is causing the problem.
I've checked my headers for any mistakes and have made several changes to see if it would fix the problem with no luck.
// RightJust class declaration that right justifies the numbers.
#ifdef RIGHTJUST_H_
#define RIGHTJUST_H_
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace System;
using namespace std;
class RightJust
{
private:
int x, totalWH1, totalWH2, totalWH3, totalWH4;
int itemT1, itemT2, itemT3, itemT4, itemT5; // Holds item totals
string WH1, WH2, WH3, WH4;
int num[4][5]; // Array containing data
public:
RightJust();
int warehouseLen();
void rightJustZero();
void rightJustLess();
};
// This program asks to user to choose a report type and displays
// the report type chosen.
#include "stdafx.h"
#include <iostream>
#include "RightJust.h"
#include <string>
#include <cstring>
using namespace System;
using namespace std;
int main
{
RightJust type;
if(reportType == 1)
{
type.rightJustZero();
}
else if(reportType == 2)
{
type.rightJustLess();
}
}
I just want a solution to get rid of all the error messages to see if my program is working how I needed it to work.
Test3.cpp
1>Test3.cpp(24): error C2065: 'RightJust' : undeclared identifier
1>Test3.cpp(24): error C2146: syntax error : missing ';' before identifier 'type'
1>Test3.cpp(24): error C2065: 'type' : undeclared identifier
1>Test3.cpp(27): error C2065: 'type' : undeclared identifier
1>Test3.cpp(27): error C2228: left of '.rightJustZero' must have class/struct/union
1> type is ''unknown-type''
1>Test3.cpp(31): error C2065: 'type' : undeclared identifier
1>Test3.cpp(31): error C2228: left of '.rightJustLess' must have class/struct/union
1> type is ''unknown-type''
The include guard in your header is wrong. It should be
#ifndef RIGHTJUST_H_
Using #ifdef RIGHTJUST_H_ will skip the code if the symbol is not defined.
You should not have #include "stdafx.h" in your .h files. And since you seem to be using Visual Studio, you can just use #pragma once instead of the include guard.

C++ .h and .cpp file separate

I'm trying to separate my c++ code to one header and one cpp file, but the interpreter showing some errors.
Here is my code:
Password.h:
#ifndef PASSWORD_H
#define PASSWORD_H
class Password {
private:
string aliasName_;
int hashOfPassword_;
public:
void setAliasName(string aliasName);
void setHashOfPassword(int hashOfPassword);
string getAliasName() { return aliasName_; }
int getHashOfPassword() { return hashOfPassword_; }
};
#endif
Password.cpp:
#include <string>
#include "Password.h"
using std::string;
void Password::setAliasName(string aliasName) {
aliasName_ = aliasName;
}
void Password::setHashOfPassword(int hashOfPassword) {
hashOfPassword_ = hashOfPassword;
}
Errors:
Error C2065 'aliasName_': undeclared identifier X\password.cpp 7
Error C2511 'void Password::setAliasName(std::string)': overloaded member function not found in 'Password' X\password.cpp 6
Error C3646 'aliasName_': unknown override specifier X\password.h 6
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int X\password.h 6
Error C2061 syntax error: identifier 'string' X\password.h 9
Error C3646 'getAliasName': unknown override specifier X\password.h 11
Error C2059 syntax error: '(' X\password.h 11
Error C2334 unexpected token(s) preceding '{'; skipping apparent function body X\password.h 11
Anyone have any ideas ?
You need to move using std::string before the declaration of your class:
#ifndef PASSWORD_H
#define PASSWORD_H
#include <string>
using std::string;
class Password {
…
and remove it from your .cpp file.
Also, you might want to use #pragma once instead of the traditional #ifndef/#define/#endif and finally you might want to make your argument and methods const when need be.
You need to move
#include <string>
using std::string;
To inside Password.h, you can then remove these two lines from Password.cpp.
Just add std:: to every string in your header file and remember, you should never use using or using namespace there! You should also include all the headers to the file in which you use them.

first time using .h and multiple .cpp files in one project, errorssss :(

second post on StackOverflow. I just have some general questions as to why my program is acting the way it is, I don't want help in completing it I was just absent from class on Friday and apparently I missed a lot.
I'm tasked to design a program that contains 3 .cpp and 2 .h files, in essence it will search and sort through arrays of strings using the bubble sort, insertion sort, selection sort methods and sequential and binary search. We are then supposed to benchmark each method to figure out which is the fastest.
I am just confused as to why the compiler keeps yelling at me, it's not making much sense I've been sitting here for about an hour fiddling around with different options or typing the code in differently but to no avail.
My header file
const int NOT_FOUND = -1;
int sequentialSearch(string a[], string needle, int length );
JohnSearch.cpp
#include "JohnSearch.h"
#include <string>
int sequentialSearch(string copied[], string needle, int length)
{
int i; // iteration variable
// look at every element to see if it is the same as needle
for( i = 0; i < length; i++ )
if( copied[i] == needle )
return i;
return NOT_FOUND;
}
TestSearch.cpp
#include "JohnSearch.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*
** printArray(title,a,length): print out title and then the contents of array a
*/
void printArray(string title, string ref[], int length )
{
int i; // array iteration
cout << title << ": \n";
for( i = 0; i < length; i++ )
cout<<setw(20)<<ref[i]<<"\n";
}
int main(void)
{
string reference[]={"John", "Allen", "Kevin", "Elisabeth"};
const int SZ=sizeof(reference)/sizeof(reference[0]);
string copied[SZ];
printArray("Reference", reference, SZ);
// sequential search (on unsorted array)
cout<<"Search.sequential(ref,Kevin):\t"<<sequentialSearch(reference, "Kevin", SZ)<<endl;
system("Pause");
return 0;
}
Errors
johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
testjohnsearch.cpp(28): error C3861: 'copyArray': identifier not found
testjohnsearch.cpp(31): error C2064: term does not evaluate to a function taking 3 arguments
johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
johnsearch.cpp(7): error C2065: 'string' : undeclared identifier
johnsearch.cpp(7): error C2146: syntax error : missing ')' before identifier 'copied'
johnsearch.cpp(7): error C2374: 'sequentialSearch' : redefinition; multiple initialization
johnsearch.h(2) : see declaration of 'sequentialSearch'
johnsearch.cpp(7): error C2059: syntax error : ')'
johnsearch.cpp(8): error C2143: syntax error : missing ';' before '{'
johnsearch.cpp(8): error C2447: '{' : missing function header (old-style formal list?)
I'm obviously doing something completely and utterly wrong. I need JohnSearch.cpp for JohnSearch.h right? The forward declaration of the function in JohnSearch.h is defined in JohnSearch.cpp so I need those two files correct?
I'm just really confused. The example program we are supposed to modify has 2 .h files and 3 .cpp files. 2 of those .cpp files correspond with the 2 .h files so thats why I assumed I would also need 2 .h files and 3 .cpp files.
String is still undefined.
johnsearch.h(2): error C2065: 'string' : undeclared identifier
Your header file uses string , so you'll need to include <string>, before your declarations. You also need to qualify it as std::string since the string class resides in the std namespace
So your header file becomes:
#include <string>
const int NOT_FOUND = -1;
int sequentialSearch(std::string a[], std::string needle, int length );
(you should also use include guards in your header files)
Your JohnSearch.cpp also uses string, again, since string is in the std namespace, you'll get errors if you don't use std::string
In your TestSearch.cpp, you have a using namespace std; at the top, you could do the same in JohnSearch.cpp too, that way you can use string instead of std::string
When in doubt, simplify. You can boil the code down to something like this:
#include "JohnSearch.h"
void sequentialSearch(string needle)
{
}
and get the same error (and maybe a warning about an unused parameter).
Yes, string is a type of variable, but it's not innate in the C++ language itself, it's in one of the standard libraries, something you have to tell the compiler about:
#include "JohnSearch.h"
#include <string>
using std::string;
void sequentialSearch(string needle)
{
}
In your header file that you include, you need to have the exact same signature than your function in the cpp file.
Also dont forget to #include <string>, and then use a string like : std::string
E.g.
Int function(int number, int number2);
And in your cpp
Int function(int number, int number2)
{
// code
}
Signature is "int function(int, int)".

A dll with a vector of vectors. in one of its classes methods

In my c++ program i'm trying to create a dll that houses the functionality of my a* algorithm.
I encounter a problem when trying to pass the map into it, I first tried to use a 2d array, but that limited my map sizes, so i'm now trying to use a vector in a vector and I keep hitting some odd snag.
In my dlls .h file:
namespace IInterface
{
class IInterface
{
public:
// Sets the map
static __declspec(dllexport) void setMap(int h, int w,vector<vector<byte>> &myarray);
private:
static vector<vector<byte>> mymap;
}
Finaly in the .cpp i have:
#include "IInterface.h"
#include <Windows.h>
#include <stdexcept>
#include <vector>
using namespace std;
namespace IInterface
{
void IInterface::setMap(int h, int w,vector<vector<byte>> &myarray)
{
mymap = myarray;
}
}
Im getting a few errors on compilation even tho the code looks fine to me.
error C2061: syntax error : identifier 'vector' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 7 1 DMAstarDLL
error C2143: syntax error : missing ';' before '<' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2238: unexpected token(s) preceding ';' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2511: 'void IInterface::IInterface::setMap(int,int,std::vector<_Ty> &)' : overloaded member function not found in 'IInterface::IInterface' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.cpp 13 1 DMAstarDLL
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
I looked at some samples, but there was really nothing that matched this scenario. I have a sneaking suspicion i'm forgetting something crucial, but I cant see it. Ideas on getting this to work?
your dlls.h does not include vector type - you should tell the compiler vector definition and include .
Tip: don't use using namespace std; in header file only in cpp. Instead of this use std::vector ...etc.
Secondly, be careful when your dll interface contains stl. This library differs as regards Release and Debug versions, so if you load Release dll in Debug program you could have problems.

error C2143: syntax error : missing ';' before '*' in Visual C++ [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am new to C++ , I created a Single Document Interface application with MS Visual C++.
when I compile it,some errors have occurred in header file called Day10 SDIDOC.h as follows
error C2143: syntax error : missing ';' before '*'
error C2501: 'CLine' : missing storage-class or type specifiers
error C2501: 'GetLine' : missing storage-class or type specifiers
error C2143: syntax error : missing ';' before '*'
error C2501: 'CLine' : missing storage-class or type specifiers
error C2501: 'AddLine' : missing storage-class or type specifiers
my files are
Day10 SDIDOC.h
public:
CLine * GetLine(int nIndex);
int GetLineCount();
CLine * AddLine(CPoint ptFrom,CPoint ptTo);
CObArray m_oaLines;
virtual ~CDay10SDIDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
these GetLine() and AddLine() methods implement like this in Day10 SDIDOC.cpp
Day10 SDIDOC.cpp
CLine * CDay10SDIDoc::AddLine(CPoint ptFrom, CPoint ptTo)
{
//create a new CLine Object
CLine *pLine = new CLine(ptFrom,ptTo);
try
{
//Add the new line to the object array
m_oaLines.Add(pLine);
//Mark the document as dirty(unsaved)
SetModifiedFlag();
}
//Did we run into a memory exception ?
catch(CMemoryException* perr)
{
//Display a message for the user,giving the bad new
AfxMessageBox("Out of Memory",MB_ICONSTOP|MB_OK);
//Did we create a line object?
if(pLine)
{
//Delete it
delete pLine;
pLine = NULL;
}
//delete the exception object
perr->Delete();
}
return pLine;
}
And GetLine Method
CLine * CDay10SDIDoc::GetLine(int nIndex)
{
return (CLine*) m_oaLines[nIndex];
}
I can't understand what wrong with.
Please Give me a solution for that.
Thank you...
It appears your compiler cannot see the declaration for CLine at the point when it parses the 2 functions. Because of this it has no idea what that name is, and thus errors out.
You can solve this by including the header to the definition of CLine or adding just a forward declaration at the top of Day10 SDIDOC.h and including the header in the cpp file. A forward declaration would suffice since you use only a pointer to CLine and do not define a CLine object or use its definition in anyway in the header.