Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 638871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:46:47+00:00 2026-05-13T20:46:47+00:00

I am learning how to work with std::vector and want to access its values

  • 0

I am learning how to work with std::vector and want to access its values and functions. I have a vector object inside another object called spectrum. Now when I try to determine the capacity of the vector using .capacity it works fine if I just declare the vector. But when I declare the vector inside another object, I get syntax errors.

The error:

test.c++:36: error: base operand of ‘->’ has non-pointer type ‘Spectrum’

As mentioned already below, -> should be a dot.

What I want is to determine the capacity of the container, and even though it now compiles it gives result 0 instead of the 8 I would expect.

The code:

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

/*  spectrum    */
class Spectrum{
    public:
        float oct;
        vector<float> band;

        float total(){
            int k;
            float lpow;
            // logarithmic summation
            for(k = 0; k < oct; k++){
                lpow = lpow + pow(10, band[k]);
            }
            return(10*log10(lpow));
        }

        Spectrum(int max_oct = 3){
            oct = max_oct;
            cout << "max oct = " << max_oct << endl;
            vector<float> band(max_oct); //create vector/array with max_oct bands
            cout << (int)band.capacity() << endl;
        }

};

int main()
{
    //does not work in a class
    Spectrum input(8);
    cout << (int)input->band.capacity() << endl;

    //does work outside of a class
    vector<float> band(8);
    cout << (int)band.capacity() << endl;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:46:47+00:00Added an answer on May 13, 2026 at 8:46 pm

    The line vector<float> band(max_oct); doesn’t do what you think it does.

    It defines an automatic variable called band in the scope of the Spectrum constructor. It doesn’t touch the member variable also called band: in fact it “hides” it, so that any later references to band in the constructor refer to the automatic variable, not the member variable (which you could access with this->band).

    What you want is:

    Spectrum(int max_oct = 3) : oct(max_oct), band(max_oct) {
    }
    

    or (less good, because it constructs an empty vector and then resizes it, rather than constructing it the right size in the first place):

    Spectrum(int max_oct = 3) {
        oct = max_oct;
        band.resize(max_oct);
    }
    

    By the way, I think you might be confusing the size and the capacity of vectors (not sure though from what you say). The single-arg constructor of vector creates a vector with the specified size. So if you don’t already, you should expect the capacity to be 8 or more, rather than 8.

    [Edit: in answer to your next question, you need to initialize lpow in total(): float lpow = 0;]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 319k
  • Answers 319k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want your points of interest to align properly… May 14, 2026 at 12:05 am
  • Editorial Team
    Editorial Team added an answer Apache's ArrayUtils has this (it still iterates behind the scenes):… May 14, 2026 at 12:05 am
  • Editorial Team
    Editorial Team added an answer Because you provided a conversion constructor. If you don't want… May 14, 2026 at 12:05 am

Related Questions

I am trying to use std::for_each to output the contents of vectors, which may
So I am currently learning C++ and decided to make a program that tests
I am trying to follow examples given in various places for D apps. Generally
I am writing a small program for my personal use to practice learning C++
I am learning how to do an ASTVisitor for a project, so I started

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.