-
vector of pointers
why can't i do this?
(not sure which includes matter so i just put them all in)
Code:
#include <ws2tcpip.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <winsock.h>
#include <sys\types.h>
#include "profileNode.h"
using namespace std;
using namespace System;
vector<profileNode*> data; //global vareable so that the vector can be easly accesed anywhere.
every where i went online said that this is the way to make a vector of pointers. but when i run it i get this one error.
"c:\users\tony\documents\visual studio 2008\projects\guiopcon\guiopcon 1.0\Form1.h(32) : error C3699: '*' : cannot use this indirection on type 'profileNode'"
compiler replacing '*' with '^' to continue parsing
if i replace the '*' with '^' i get 2 errors
"c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xmemory(52) : error C3698: 'profileNode ^' : cannot use this type as argument of 'new'
did you mean 'profileNode' (without the top-level '^')?"
...(bunch of other stuff under this message)...
and
"c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xmemory(52) : error C3828: 'profileNode': placement arguments not allowed while creating instances of managed classes"
how can i do this. i also ran into problems using just "profileNode" with out a '^' or '*'
-
* is C++.
^ is Managed C++.
You'll have to pick one, and you appear to have picked the second, somehow, based on your compiler messages. (Probably that's what profileNode is written in.) If you pick the second one, you need to be consistent throughout (hence the slap-on-the-wrist for trying to use new).
-
i dont understand b/c i didnt use "new" in my program. it says new is in "xmemory" idk what that does but it is an include and i dont think i sould change it. as for my profileNode class it doesnt use ether one as far as i can tell. this would be the first class i wrote in c++ (to be used as a part not as main) so i coudl have messed it up. its all in the .h file.
.h file
Code:
#pragma once
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
ref class profileNode
{
public:
int lineNumber;
char* projectName;
bool read;
int saveOptions;
char* lastSubject;
char* lastCondition;
profileNode(void)
{
lineNumber = -1;
projectName = "none";
read = FALSE;
saveOptions = -1;
lastSubject = "none";
lastCondition = "none";
}
profileNode(int last, char* name)
{
lineNumber = last++;
projectName = (char*) malloc(sizeof(char) * (strlen(name)+1));
strcpy(projectName, name);
read = TRUE;
saveOptions = 1;
lastSubject = "ss#0000";
lastCondition = "NONE";
}
profileNode(int line, char* name, bool read1, int save, char* subject, char* condition)
{
lineNumber = line;
projectName = (char*) malloc(sizeof(char) * (strlen(name)+1));
strcpy(projectName, name);
read = read1;
saveOptions = save;
lastSubject = (char*) malloc(sizeof(char) * (strlen(subject)+1));
strcpy(lastSubject, subject);
lastCondition = (char*) malloc(sizeof(char) * (strlen(condition)+1));
strcpy(lastCondition, condition);
}
#pragma endregion
public: bool compareNode(char* name)
{
//go thought linked list to find the node we want
if(strcmp(name, this->projectName) == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
};
.cpp file
Code:
#include "StdAfx.h"
#include "profileNode.h"
-
Code:
ref class profileNode
No such thing in C++. That is something you would have in Managed C++. I don't think Managed can use the STL (vector etcetera) because as you noticed the STL uses its own memory management. I don't speak Managed myself, but a quick Google search turned up some alternatives.
EDIT: And reading through the code: if you don't want managed, then get rid of "ref". If you are including <string>, why not use it instead of using char* everywhere? The keywords "true" and "false" are spelled just like that, not in capitals.
-
thank you so much. i removed ref and it worked, kinda, it still crashed but but during run time. as for the other stuff... i copied over string from the other class the reason i dont use it is because this is my first program in c++ and i dont really know how to use it. and true and false work in caps as well and i like then that way b/c there easier to see.
-
Your crash is due to you abusing C stuff in C++ unsafely. Get rid of them. Use std::string instead of char*, and new instead of malloc.
Heck, with strings, you don't need malloc anyway (or new).
-
You really need to decide which language you're programming in and learn how to use it.
Simply throwing together random bits of syntax from across the whole 'C-ish' spectrum isn't the way to go.
It might well compile (actually, getting anything to compile is as easy as falling off a log). The real trick is writing something that will actually work.
Here's one pitfall.
Code:
lastSubject = "ss#0000";
...
lastSubject = (char*) malloc(sizeof(char) * (strlen(subject)+1));
The huge problem here is, how will you clean this up without leaking memory, or trying to free a const char array?