Like Tree1Likes
  • 1 Post By Elysia

vector of pointers

This is a discussion on vector of pointers within the C++ Programming forums, part of the General Programming Boards category; why can't i do this? (not sure which includes matter so i just put them all in) Code: #include <ws2tcpip.h> ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    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 '*'

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    * 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).

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    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"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    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.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    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).
    Salem likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector containing pointers to another vector
    By yes2 in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2010, 02:55 PM
  2. Vector of pointers to vector of objects
    By Litz in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2009, 02:29 PM
  3. vector of pointers
    By gamer4life687 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 10:49 PM
  4. vector of pointers vs vector of values
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2002, 01:27 PM
  5. Vector of pointers
    By Hunter2 in forum Windows Programming
    Replies: 5
    Last Post: 08-19-2002, 09:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21