Hey there, working on a program thats going to have a bunch of functions, but for now I am stuck on something that will be reused throughout the program alot: passing a string pointer to a function.

It may seem redundant or arbitrary that I am passing it as a pointer, but is required for the project in question.

I am not sure what I am doing wrong here, but it is returning the error:

In function `void charcount(std::string*)':
`length' has not been declared
request for member of non-aggregate type before '(' token

- for line 67 (which is the cout in the charcount function) -

I would really appreciate any and all advice/help on this.

Thanks for your time.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//prototypes
void writestart();
void readstart(string &str1, string &str2, string &str3);
void charcount(string *);

int main()
{
    string str1, str2, str3;
    string *strptr1, *strptr2, *strptr3;
    
    strptr1 = &str1;
    strptr2 = &str2;
    strptr3 = &str3;
    
    writestart();
    readstart(str1, str2, str3);
    
    cout << endl << str1 << endl
         << str2 << endl
         << str3 << endl << endl;
         
    charcount(strptr1);
}

//Starting Function that asks for the strings from user input to write to file for later usage.
void writestart()
{
    ofstream wfile;
    string str1, str2, str3;
    
    cout << "Please enter your first string:" << endl;
    getline (cin,str1);
    
    cout << "Please enter your second string:" << endl;
    getline (cin,str2);
    
    cout << "Please enter your third string:" << endl;
    getline (cin,str3);
    
    wfile.open("Stringfile.txt");
    wfile << str1 << endl
          << str2 << endl
          << str3 << endl;
    wfile.close();
}

//Function that reads the strings in for the program to use/format etc...
void readstart(string &str1, string &str2, string &str3)
{
    ifstream rfile("Stringfile.txt");
    getline(rfile, str1);
    getline(rfile, str2);
    getline(rfile, str3);
    rfile.close();
}

//Determines and returns the number of characters in a string.
void charcount(string *str)
{
    cout << endl << "Chars: " << *str.length() << endl;
}

Recommended Answers

All 2 Replies

The dereference (*) operator has lower priority than the member (.) operator so you need to add brackets:

(*str).length()

I had no idea there could be a confliction like that, and so simple hahaha.

Thank you very much, I must seem like an idiot missing that but I honestly didnt know that could happnen, I am still pretty new to pointers and such but yea... Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.